JavaScript专业八级测试
javascript ["1", "2", "3"].map(parseInt)
解析:答案为:[1, NaN, NaN]1
2
3["1","2","3"].map(parseInt("1",0)) //1
["1","2","3"].map(parseInt("2",1)) //NaN
["1","2","3"].map(parseInt("3",2)) //NaNjavascript [typeof null, null instanceof Object]
This is actually a long-standing bug in JavaScript, but it has not been fixed. null is not an object. But the result of typeof null is “object”.
null is not an instance of Object
答案为:['object' false]
javascript [ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)]
解析:答案为:error1
2
3[3,2,1].reduce((acc,cur)=>Match.pow(3,2)) //9
[3,2,1].reduce((acc,cur)=>Match.pow(9,1))//9
[].reduce(Math.pow)//报错,由于 reduce 没有初始值javascript var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
解析:
由于 + 的运算符优先级高于三元运算符,所以会先执行'Value is ' + (val === 'smtg')// Value is true
后执行'Value is true'?'Something' : 'Nothing'
所以最终结果为:’Something’
答案为:Something
- code: 解析:
1
2
3
4
5
6
7
8
9var name = 'World!';
(function () {
if (typeof name === 'undefined') {
var name = 'Jack';
console.log('Goodbye ' + name);
} else {
console.log('Hello ' + name);
}
})();
以上自执行函数形成了一个闭包,闭包会形成一个独立的作用域环境,val 声明的变量存在变量提升,所以typeof name === 'undefined'
返回结果为true
最终输出结果为:'Goodbye Jack'
答案为:’Goodbye Jack’ - code: 解析:
1
2
3
4
5
6
7var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
count++;
}
console.log(count);
变量START
和END
和差值为 100,也就是 for 循环会循环 100次,最终结果为: 100
答案为:100 - code: 答案为:[]
1
2
3var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;}); - code: 解析:
1
2
3
4
5var two = 0.2;
var one = 0.1;
var eight = 0.8;
var six = 0.6;
[two - one == one, eight - six == two]
由于浮点数的精度问题,所以two - one == one
返回结果为true
,eight - six == two
0.8 - 0.6 equals 0.20000000000000007 返回结果为false
答案为:[true false]
- code: 解析:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16function showCase(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase(new String('A'));
由于new String('A')
返回的是一个对象 String{‘A’},所以switch
语句会返回Do not know!
答案为:’Do not know!’ - code: 解析:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16function showCase2(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase2(String('A'));
由于String('A')
返回的是一个字符串 ‘A’,所以switch
语句会返回Case A
答案为:’Case A’ - code: 解析:
1
2
3
4
5
6
7
8
9
10
11function isOdd(num) {
return num % 2 == 1;
}
function isEven(num) {
return num % 2 == 0;
}
function isSane(num) {
return isEven(num) || isOdd(num);
}
var values = [7, 4, '13', -9, Infinity];
values.map(isSane);
由于答案为:1
2
3
4
57%2==0 || 7%2 == 1 //true
4%2==0 || 4%2 == 1 //true
'13'%2==0 || '13'%2 == 1 //true
-9%2==0 || -9%2 == 1 //false
Infinity%2==0 || Infinity%2 == 1 //false[true, true, true, false, false]
- code parseInt(3, 8) -> 3, 3 是一个有效的八进制数,结果为:
1
2
3parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)3
parseInt(3, 2) -> NaN, 3 不是一个有效的二进制数,结果为:NaN
parseInt(3, 0) -> 3, 0 代表十进制,结果为:3
答案为:3 NaN 3
- code:答案为:
1
Array.isArray( Array.prototype )
true
- code:答题解析:
1
2
3
4
5
6var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log("wut");
}
在 if() 语句中,所有值都会被转换成 boolean 类型,所以[0]
会被转换成true
,所以会进入console.log(a == true);
当比较数组和布尔值时,两边都会被转换为数字,[0].toString() -> ‘0’转换为数字 -> 0, 0 == true -> false, 所以最终结果为:false
答案为:false
- code:答案为:
1
[]==[]
false
- code:答案为:
1
2'5' + 3
'5' - 3'53' 2
- code:解析:
1
1 + - + + + - + 1
1+(- + + + - + 1) -> 1 + - + + + - (+ 1) -> 1 + - + + + (- + 1)->1 + - (+ + + - + 1)->1 + - -1 -> 1 + (- - 1)->1 + 1 -> 2
答案为:2
- code:数组长度为 3,但是只有一个元素,所以最终结果为:
1
2
3var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return '1'; });['1',,]
答案为:[‘1’,,] - code:解析:
1
2
3
4
5
6
7
8
9function sidEffecting(ary) {
ary[0] = ary[2];
}
function bar(a,b,c) {
c = 10
sidEffecting(arguments);
return a + b + c;
}
bar(1,1,1)在非严格模式下sidEffecting(arguments)
会改变arguments
的值,所以最终结果为:21
答案为:21 - code:因为 a 是一个 超过 2^53 的大整数,导致 a 的精度丢失,所以最终结果为:
1
2
3var a = 111111111111111110000,
b = 1111;
a + b;111111111111111110000
答案为:111111111111111110000 - code:Number.MIN_VALUE 是 JavaScript 中最小的正数,所以最终结果为:
1
Number.MIN_VALUE > 0
true
答案为:true
, - code:解析:
1
[1 < 2 < 3, 3 < 2 < 1]
由于<
1 < 2 < 3->true < 3 -> true, 3 < 2 < 1 -> false < 1 -> true 所以结果为:[true true]
答案为:[true true] - code:答题解析:
1
2 == [[[2]]]
[[[2]]].toString()->’2’ 2 == ‘2’ -> true 所以最终结果为:true
答案为:true - code:解析:
1
2
33.toString()
3..toString()
3...toString()
点运算符会被优先识别为数字常量的一部分,然后才是对象属性访问符:3.toString()
会被解析为:(3.)toString()
,所以会报错3..toString()
会被解析为(3.).toString()
,结果为:’3’3...toString()
会被解析为(3.)..toString()
,会报错
答案为:’报错,’3’,报错’ - code:解析:
1
2
3
4
5(function(){
var x = y = 1;
})();
console.log(y);
console.log(x);
由于var x = y = 1
会被解析为y = 1; var x = y;
所以y
会被定义为全局变量,x
会被定义为局部变量,所以最终结果为:1 undefined
答案为:’1 undefined’ - code:解析:
1
2
3var a = /123/, b = /123/;
a == b
a === b
由于正则表达式是对象,所以a == b
和a === b
会比较两个对象的引用,所以最终结果为:false false
答案为:false false
- code:解析:
1
2
3
4
5
6
7var a = [1, 2, 3],
b = [1, 2, 3],
c = [1, 2, 4]
a == b
a === b
a > c
a < c
由于数组是对象,所以a == b
和a === b
会比较两个对象的引用,所以a == b 和 a === b 结果为:false false
由于数组会被转换为字符串,所以a > c
和a < c
会比较两个数组的字符串形式,a -> ‘1,2,3’ c -> ‘1,2,4’,两个字符串比较如果首字母相等,会继续比较下一个字符,直到比较完成 所以最终结果为:false true
答案为:false true
- code:解析:
1
2var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]
由于对象没有prototype
属性,所以a.prototype
会返回undefined
,所以最终结果为:[false true]
答案为:[false true]
- code:解析:
1
2
3function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b
由于函数的prototype
属性是一个对象,而Object.getPrototypeOf(f)
是函数,所以最终结果为:false
答案为:false
- code:解析:
1
2
3
4function foo() { }
var oldName = foo.name;
foo.name = "bar";
[oldName, foo.name]
The result shows["foo", "foo"]
. This demonstrates that:
The initial foo.name is “foo” (which gets stored in oldName)
Even though we try to change foo.name to “bar”, it remains “foo”
This is because the name property of functions in JavaScript is read-only by default (it’s a non-writable property). In strict mode, attempting to change it would throw an error. In non-strict mode, the assignment is silently ignored.Object.getOwnPropertyDescriptor(foo, 'name').writable // false
答案为:["foo", "foo"]
- code:解析:
1
"1 2 3".replace(/\d/g, parseInt)
First digit: parseInt(“1”, 0) → 1 (radix 0 is treated as 10)
Second digit: parseInt(“2”, 2) → NaN (invalid binary digit)
Third digit: parseInt(“3”, 4) → 3 (valid in base 4)
So the result is: “1 NaN 3”
答案为:"1 NaN 3"
- code:解析:
1
2
3
4
5
6function f() {}
var parent = Object.getPrototypeOf(f);
f.name // ?
parent.name // ?
typeof eval(f.name) // ?
typeof eval(parent.name) // ?
f.name -> “f”
parent.name -> ‘’
typeof eval(f.name) -> “function”
typeof eval(parent.name) -> “undefined” - code:解析:
1
2var lowerCaseOnly = /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]
The result is[true, true]
. This demonstrates that:test在检测时会隐性将内容转为字符串,其实等同于:’[lowerCaseOnly.test(‘null’), lowerCaseOnly.test(‘undefined’)]’
答案为:[true, true]
- code:解析:
1
[,,,].join(", ")
The result is", , "
. This demonstrates that:
Empty array slots are treated as undefined
When .join() is called with a separator, it places the separator between each element
In this case, there are three slots, so two separators are inserted
So the output is literally:, ,
答案为:, ,
- code:解析:
1
2var a = {class: "Animal", name: 'Fido'};
a.class
The result is"Animal"
. This demonstrates that:This creates an object a with two properties:
class with the value “Animal”
name with the value “Fido”答案为:
“Animal”`