参考地址:http://javascript-puzzlers.herokuapp.com/
1. ["1", "2", "3"].map(parseInt)
返回值为[1, NaN, NaN] .因为parseInt接受两个参数,即parseInt('1', 0), parseInt('2', 1),parseInt('3', 2)
2.[typeof null, null instanceof Object]
返回值["object", false]。
在JavaScript的实现中,JavaScript值被表示为类型标记和值。对象的类型标记为0。null被表示为空指针(在大多数平台中是0x00)。因此,null的类型标记为0,因此返回值的伪类型。
而instanceof查的是null的prototype属性,但是它没有。
3.[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
返回值an error,因为reduce归并的数组必须大于等于1
4.var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
返回值 'Something'。考察的是运算符的优先级
第一步执行小括号里的(val === 'smtg'),即 console.log('Value is ' + true ? 'Something' : 'Nothing');
第二步执行字符串拼接,即 console.log('Value is true' ? 'Something' : 'Nothing');-- 三目运算符的优先级比较低
5.var name = 'World!';
(function () {
if (typeof name === 'undefined')
{ var name = 'Jack'; console.log('Goodbye ' + name); }
else { console.log('Hello ' + name); }
})();
执行结果:’Goodbye Jack‘,自执行函数是一个局部作用域,于是 name会在局部进行声明提升,即
var name = 'World!';
(function () {
var name
if (typeof name === 'undefined')
{ name = 'Jack'; console.log('Goodbye ' + name); }
else { console.log('Hello ' + name); }
})();
6.var END = Math.pow(2, 53);
var START = END - 100; var count = 0; for (var i = START; i <= END; i++) { count++; }
console.log(count);
执行结果是死循环,应该JavaScript最大的整数为Math.pow(2, 53);,也就是说i 循环100次后会一直等于Math.pow(2, 53);
条件i <= END恒成立
7.var ary = [0,1,2]; ary[10] = 10; ary.filter(function(x) { return x === undefined;});
返回值为[],因为filter会自动过滤数组中不存在的项,连函数也不调用(注意如果项存在,值为undefined是不会过滤的)。
8.var two = 0.2 var one = 0.1 var eight = 0.8 var six = 0.6
[two - one == one, eight - six == two]
执行结果[true, false]因为JavaScript在执行小数运算的时候,有时会丢失精度。
9.function 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'));
执行结果'Do not know!',因为new String('A')是一个字符串对象,而switch进行的是===比较,不会发生类型转换。
10.function 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'));
执行结果:Case A,因为少了关键字new,只是简单的转换为字符串。
11.function 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);
执行结果:[true, true, true, false, false]。本质上就是判断一个数是正整数。-9 % 2 = -1; 与0或1都不想等。Infinity与任何数取余数都是NaN。
12.parseInt(3, 8) parseInt(3, 2) parseInt(3, 0)
执行结果是:3 NaN 3.第三个是因为传0默认10进制了
13.Array.isArray( Array.prototype )
执行结果为true,在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype中Array.prototype本身是一个数组
14.var a = [0]; if ([0]) { console.log(a == true); } else { console.log("wut"); }
执行结果为:false 首先[0]是一个对象,于是在if判断时为true。然后a == true => '0' == true => 0 == 1
15.[]==[]
执行结果: false,当等式两边都是对象时,是比较引用空间是否一致,== 与=== 效果一致,不会发生类型转换
16. '5' + 3
'5' - 3
执行结果: ‘53’,2
17. 1 + - + + + - + 1
执行结果:2. 应该操作符中间有空格,运算符当成0来处理了,于是 1 + 0 - 0 + 0 +0 + 0 - 0 + 1
18. var ary = Array(3); ary[0]=2 ary.map(function(elem) { return '1'; });
执行结果:[2, empty * 2] 因为 map会返回同长度的数组
19. function sidEffecting(ary) { ary[0] = ary[2]; }
function bar(a,b,c) { c = 10 sidEffecting(arguments); return a + b + c; }
bar(1,1,1)
执行结果: 21
20.var a = 111111111111111110000, b = 1111; a + b;
执行结果:111111111111111110000,这个是整数的最大值,加一个正整数还是它本身
21.var x = [].reverse; x();
执行结果:error , ?? 以前给的答案是window。和其他函数直接在浏览器中调用没有区别。
22. Number.MIN_VALUE > 0
执行结果:true 因为Number.MIN_VALUE是大于0的最小数
23. [1 < 2 < 3, 3 < 2 < 1]
执行结果:[true, true] . => [true < 3, false < 1]
24. 2 == [[[2]]]
执行结果: true => 2 == '2'
25. 3.toString() 3..toString() 3...toString()
执行结果: error, "3", error ??
26. (function(){ var x = y = 1; })(); console.log(y); console.log(x);
执行结果: 1 error 因为:
var y
(function(){ y = 1;var x = y ; })();
console.log(y); console.log(x);
27. var a = /123/, b = /123/; a == b a === b
执行结果: false false 因为:正则表达式是一个对象
28. var a = [1, 2, 3], b = [1, 2, 3], c = [1, 2, 4]
a == b
a === b
a > c
a < c
执行结果:false, false, false, true 因为:前两个比较是对象比较,后面数组比大小是按照字符比较
29. var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]
执行结果:[false, true] 因为:a.prototype 值为undefined, 后者是获取a的原型为Object.prototype;
30. function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b
执行结果:false 因为:Object.getPrototypeOf(f); 获取的是f构造函数上的prototype属性
31. function foo() { }
var oldName = foo.name; foo.name = "bar";
[oldName, foo.name]
执行结果:["foo", "foo"]因为:函数的名字不可修改,但修改也不会报错
32."1 2 3".replace(/\d/g, parseInt)
执行结果:"1 NaN 3" 。执行一下 "1 2 3".replace(/\d/g, console.log) 就知道了
33. function f() {}
var parent = Object.getPrototypeOf(f);
f.name // ?
parent.name // ?
typeof eval(f.name) // ?
typeof eval(parent.name) // ?
执行结果:"f" "" "function" "undefined"
34. var lowerCaseOnly = /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]
执行结果:[true, true] => [lowerCaseOnly.test(‘null’), lowerCaseOnly.test('undefined')]
35. [,,,].join(", ")
执行结果:", ,"
36. var a = {class: "Animal", name: 'Fido'};
a.class
执行结果:chrom下为"Animal",ie可能不同
37. var a = new Date("epoch")
执行结果: Invalid Date。 参数是无效的,必须是一个数字
38. var a = Function.length,
b = new Function().length
a === b
执行结果: false 因为函数.length,得到是函数的形参。Function.length为1,(new Function()).length => (function f(){}; f.lenght)形参为0
39. var a = Date(0);
var b = new Date(0);
var c = new Date();
[a === b, b === c, a === c]
执行结果:[false, false, false] 第一个返回当前时间的字符串,第二个返回起始时间对象,第三个返回当前时间对象。
40. var min = Math.min(), max = Math.max()
min < max
执行结果:false,因为Math.min
returns +Infinity,Math.max
returns -Infinity
.
41. var match = re.exec(str); return match && match[1]; }
var numRe = /num=(\d+)/ig, wordRe = /word=(\w+)/i,
a1 = captureOne(numRe, "num=1"),
a2 = captureOne(wordRe, "word=1"),
a3 = captureOne(numRe, "NUM=2"),
a4 = captureOne(wordRe, "WORD=2");
[a1 === a2, a3 === a4]
执行结果:[true, false]
42. var a = new Date("2014-03-19"),
b = new Date(2014, 03, 19);
[a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]
执行结果:[false, false] 因为:a => Wed Mar 19 2014 08:00:00 GMT+0800 (中国标准时间) b => Sat Apr 19 2014 00:00:00 GMT+0800 (中国标准时间)
43. if ('http://giftwrapped.com/picture.jpg'.match('.gif'))
{ 'a gif file' }
else { 'not a gif file' }
执行结果:'a gif file' 因为.被当成了元字符
44. function foo(a) { var a; return a; }
function bar(a) { var a = 'bye'; return a; }
[foo('hello'), bar('hello')]
执行结果:["hello", "bye"]