JavaScript选择题解答(21-37)

上半部:JavaScript选择题解答(1-20)

个人博客文章地点

第二十一题

What is the result of this expression? (or multiple ones)

var a = 111111111111111110000,
    b = 1111;
a + b;

A: 111111111111111111111

B: 111111111111111110000

C: NaN

D: Infinity

又是一道考核JavaScript数字的题,与第七题考核点相似。由于JavaScript实际上只要一种数字情势IEEE 754规范的64位双精度浮点数,其所能示意的整数局限为-2^53~2^53(包含边境值)。这里的111111111111111110000已超过了2^53次方,所以会发作精度丧失的状况。综上选B

第二十二题

What is the result of this expression? (or multiple ones)

var x = [].reverse;
x();

A: []

B: undefined

C: error

D: window

这题考核的是函数挪用时的this和Array.prototype.reverse要领。

起首看Array.prototype.reverse要领,起首举几个栗子:

console.log(Array.prototype.reverse.call("skyinlayer"));
//skyinlayer
console.log(Array.prototype.reverse.call({}));
//Object {}
console.log(Array.prototype.reverse.call(123));
//123

这几个栗子能够得出一个结论,Array.prototype.reverse要领的返回值,就是this

Javascript中this有以下几种状况:

全局下this,指向window对象

console.log(this);
//输出效果:
//Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

函数挪用,this指向全局window对象:

function somefun(){
    console.log(this);
}
somefun();
//输出效果:
//Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

要领挪用,this指向具有该要领的对象:

var someobj = {};
someobj.fun = function(){
    console.log(this);
};
console.log(someobj.fun());
//输出效果:
//Object {fun: function}

挪用组织函数,组织函数内部的this指向新建立对象:

function Con() {
    console.log(this);
}
Con.prototype.somefun = function(){};
console.log(new Con());
//输出效果:
//Con {somefun: function}

显现肯定this:

function somefun(){
    console.log(this);
};
somefun.apply("skyinlayer");
somefun.call("skyinlayer");
//输出效果:
//String {0: "s", 1: "k", 2: "y", 3: "i", 4: "n", 5: "l", 6: "a", 7: "y", 8: "e", 9: "r", length: 10}
//String {0: "s", 1: "k", 2: "y", 3: "i", 4: "n", 5: "l", 6: "a", 7: "y", 8: "e", 9: "r", length: 10} 

这里能够看到,运用的是函数挪用体式格局,this指向的是全局对象window,所以选D

第二十三题

What is the result of this expression? (or multiple ones)

Number.MIN_VALUE > 0

A: false

B: true

C: error

D: other

考核的Number.MIN_VALUE的观点,MDN传送门,症结的几句话
* The Number.MIN_VALUE property represents the smallest positive numeric value representable in JavaScript.
翻译:Number.MIN_VALUE示意的是JavaScript中最小的正数

  • The MIN_VALUE property is the number closest to 0, not the most negative number, that JavaScript can represent.
    翻译:MIN_VALUE是靠近0的数,而不是最小的数

  • MIN_VALUE has a value of approximately 5e-324. Values smaller than MIN_VALUE (“underflow values”) are converted to 0.
    翻译:MIN_VALUE值约等于5e-324,比起更小的值(大于0),将被转换为0

所以,这里是true,选B

顺带把Number的几个常量拉出来:
* Number.MAX_VALUE:最大的正数
* Number.MIN_VALUE:最小的正数
* Number.NaN:特别值,用来示意这不是一个数
* Number.NEGATIVE_INFINITY:负无穷大
* Number.POSITIVE_INFINITY:正无穷大

假如要示意最小的负数和最大的负数,能够运用-Number.MAX_VALUE和-Number.MIN_VALUE

第二十四题

What is the result of this expression? (or multiple ones)

[1 < 2 < 3, 3 < 2 < 1]

A: [true, true]

B: [true, false]

C: error

D: other

运算符的运算递次和隐式范例转换的题,从MDN上运算符优先级,'<‘运算符递次是从左到右,所以变成了[true < 3, false < 1]

接着举行隐式范例转换,'<‘操纵符的转换划定规矩(来自$雨$的文章《Javascript范例转换的划定规矩》):

  1. 假如两个操纵值都是数值,则举行数值比较
  2. 假如两个操纵值都是字符串,则比较字符串对应的字符编码值
  3. 假如只要一个操纵值是数值,则将另一个操纵值转换为数值,举行数值比较
  4. 假如一个操纵数是对象,则挪用valueOf()要领(假如对象没有valueOf()要领则挪用toString()要领),获得的效果依据前面的划定规矩实行比较
  5. 假如一个操纵值是布尔值,则将其转换为数值,再举行比较

所以,这里起首经由过程Number()转换为数字然后举行比较,true会转换成1,而false转换成0,就变成了[1 < 3, 0 < 1]

所以效果为A

第二十五题

What is the result of this expression? (or multiple ones)

// the most classic wtf
2 == [[[2]]]

A: true

B: false

C: undefined

D: other

又是隐式范例转换的题(汗)

题目作者的诠释是:
both objects get converted to strings and in both cases the resulting string is “2”

也就是说摆布双方都被转换成了字符串,而字符串都是”2″

这里起首须要对==右边的数组举行范例转换,依据以下划定规矩(来自justjavac的文章《「译」JavaScript 的怪癖 1:隐式范例转换》):
1. 挪用 valueOf()。假如效果是原始值(不是一个对象),则将其转换为一个数字。
2. 不然,挪用 toString() 要领。假如效果是原始值,则将其转换为一个数字。
3. 不然,抛出一个范例毛病。

所以右边被运用toString()要领转换为”2″,然后又经由过程Number(“2”)转换为数字2举行比较,效果就是true了,选A

第二十六题

What is the result of this expression? (or multiple ones)

3.toString()
3..toString()
3...toString()

A: “3”, error, error

B: “3”, “3.0”, error

C: error, “3”, error

D: other

说实话这题有点常见了,许多人都踩过3.toString()的坑(包含我)…虽然JavaScript会在挪用要领时对原始值举行包装,然则这个点是小数点呢、照样要领挪用的点呢,于是乎第一个就是error了,由于JavaScript诠释器会将其认为是小数点。

而第二个则很好说通了,第一个点诠释为小数点,变成了(3.0).toString(),效果就是”3″了

第三个也是,第一个点为小数点,第二个是要领挪用的点,然则背面接的不是一个正当的要领名,于是乎就error了

综上,选C

第二十七题

What is the result of this expression? (or multiple ones)

(function(){
  var x = y = 1;
})();
console.log(y);
console.log(x);

A: 1, 1

B: error, error

C: 1, error

D: other

变量提拔和隐式定义全局变量的题,也是一个JavaScript典范的坑…

照样那句话,在作用域内,变量定义和函数定义会先行提拔,所以内里就变成了:

(function(){
    var x;
    y = 1;
    x = 1;
})();

这点会问了,为何不是var x, y;,这就是坑的处所…这里只会定义第一个变量x,而y则会经由过程不运用var的体式格局直接运用,于是乎就隐式定义了一个全局变量y

所以,y是全局作用域下,而x则是在函数内部,效果就为1, error,选C

第二十八题

What is the result of this expression? (or multiple ones)

var a = /123/,
    b = /123/;
a == b
a === b

A: true, true

B: true, false

C: false, false

D: other

起首须要明白JavaScript的正则表达式是什么。JavaScript中的正则表达式依旧是对象,运用typeof运算符就可以得出效果:

console.log(typeof /123/);
//输出效果:
//"object"

==运算符摆布双方都是对象时,会比较他们是不是指向统一个对象,能够明白为C语言中两个指针的值是不是一样(指向统一片内存),所以两个效果天然都是false

第二十九题

What is the result of this expression? (or multiple ones)

var a = [1, 2, 3],
    b = [1, 2, 3],
    c = [1, 2, 4]
a == b
a === b
a > c
a < c

A: false, false, false, true

B: false, false, false, false

C: true, true, false, true

D: other

和上题相似,JavaScript中Array的实质也是对象,所之前两个的效果都是false,

而JavaScript中Array的’>’运算符和'<‘运算符的比较体式格局相似于字符串比较字典序,会从第一个元素最先举行比较,假如一样比较第二个,还一样就比较第三个,云云类推,所以第三个效果为false,第四个为true。

综上所述,效果为false, false, false, true,选A

第三十题

What is the result of this expression? (or multiple ones)

var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]

A: [false, true]

B: [true, true]

C: [false, false]

D: other

原型链的题(总会有的),考核的__proto__和prototype的区分。起首要明白对象和组织函数的关联,对象在建立的时刻,其__proto__会指向其组织函数的prototype属性

Object实际上是一个组织函数(typeof Object的效果为”function”),运用字面量建立对象和new Object建立对象是一样的,所以a.__proto__也就是Object.prototype,而Object.getPrototypeOf(a)与a.__proto__是一样的,所以第二个效果为true

而实例对象是没有prototype属性的,只要函数才有,所以a.prototype实际上是undefined,第一个效果为false

综上,选A

第三十一题

What is the result of this expression? (or multiple ones)

function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b

A: true

B: false

C: null

D: other

照样__proto__和prototype的区分,二者不是一个东西,所以选B

第三十二题

What is the result of this expression? (or multiple ones)

function foo() { }
var oldName = foo.name;
foo.name = "bar";
[oldName, foo.name]

A: error

B: [“”, “”]

C: [“foo”, “foo”]

D: [“foo”, “bar”]

考核了函数的name属性,运用函数定义体式格局时,会给function对象自身增加一个name属性,保留了函数的称号,很好明白oldName为”foo”。name属性时只读的,不允许修正,所以foo.name = "bar";以后,foo.name照样”foo”,所以效果为[“foo”, “foo”],选C

PS:name属性不是一个规范属性,不要去运用,除非你想要坑他人

第三十三题

What is the result of this expression? (or multiple ones)

"1 2 3".replace(/\d/g, parseInt)

A: “1 2 3”

B: “0 1 2”

C: “NaN 2 3”

D: “1 NaN 3”

String.prototype.replace、正则表达式的全局婚配和parseInt(又是parseInt…),能够依据题意看出来题目上漏了一个’\’

起首须要肯定replace会传给parseInt哪些参数。举个栗子:

"1 2 3".replace(/\d/g, function(){
    console.log(arguments);
});
//输出效果:
//["1", 0, "1 2 3"]
//["2", 2, "1 2 3"]
//["3", 4, "1 2 3"] 

一共三个:
1. match:正则表达式被婚配到的子字符串
2. offset:被婚配到的子字符串在原字符串中的位置
3. string:原字符串

如许就很好明白了,又回到之前parseInt的题目了,效果就是parseInt(“1”, 10), parseInt(“2”, 2), parseInt(“3”, 4)所以效果为”1, NaN, 3″,选D

第三十四题

What is the result of this expression? (or multiple ones)

function f() {}
var parent = Object.getPrototypeOf(f);
f.name // ?
parent.name // ?
typeof eval(f.name) // ?
typeof eval(parent.name) //  ?

A: “f”, “Empty”, “function”, “function”

B: “f”, undefined, “function”, error

C: “f”, “Empty”, “function”, error

D: other

又是Function.name属性的题,和三十二题一样样,f.name值为”f”,而eval(“f”)则会输出f函数,所以效果为”function”

接着看parent,parent实际上就是f.__proto__,须要明白的是JavaScript中的函数也是对象,其也有本身的组织函数Function,所以f.__proto__ === Function.prototype效果是true,而Function.prototype就是一个名为Empty的function

console.log(Function.prototype);
console.log(Function.prototype.name);
//输出效果:
//function Empty() {}
//Empty

所以parent.name的值为Empty

假如想直接在全局作用域下挪用Empty,显现未定义…由于Empty并不在全局作用域下

综上所述,效果为C

第三十五题

What is the result of this expression? (or multiple ones)

var lowerCaseOnly =  /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]

A: [true, false]

B: error

C: [true, true]

D: [false, true]

正则表达式的test要领会自动将参数转换为字符串,原式就变成了[lowerCaseOnly.test("null"), lowerCaseOnly.test("undefined")],效果都是真,所以选C

第三十六题

What is the result of this expression? (or multiple ones)

[,,,].join(", ")

A: “, , , “

B: “undefined, undefined, undefined, undefined”

C: “, , “

D: “”

JavaScript中运用字面量建立数组时,假如最末端有一个逗号’,’,会背省略,所以实际上这个数组只要三个元素(都是undefined):

console.log([,,,].length);
//输出效果:
//3

而三个元素,运用join要领,只须要增加两次,所以效果为”, , “,选C

第三十七题

What is the result of this expression? (or multiple ones)

var a = {class: "Animal", name: 'Fido'};
a.class

A: “Animal”

B: Object

C: an error

D: other

典范坑中的一个,class是症结字。依据浏览器的差别,效果差别:

chrome的效果: “Animal”

Firefox的效果:”Animal”

Opera的效果:”Animal”

IE 8以上也是: “Animal”

IE 8 及以下: 报错

总结

终究把37题悉数弄完了,虽然许多题都偏而怪,但个中触及的学问照样相称主要的。JavaScript中的糟粕和英华永远是一个话题,也是笔试面试时常常碰到的题目。

    原文作者:天镶
    原文地址: https://segmentfault.com/a/1190000000406574
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞