注* 本文翻译自
JavaScript Puzzlers! do you really know JavaScript?
(JavaScript迷题!你真的懂JavaScript吗?) 。本文考察了很多JavaScript中生僻冷门用法和许多容易忽视混淆的概念,据多名Hacker News网友回复,做第二遍依然无法全部答对,jser专家们可以尝试一下。
-
["1", "2", "3"].map(parseInt)
-
[typeof null, null instanceof Object]
-
[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)] ]
-
var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
-
var name = 'World!'; (function () { if (typeof name === 'undefined') { var name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); } })();
-
var END = Math.pow(2, 53); var START = END - 100; var count = 0; for (var i = START; i <= END; i++) { count++; } console.log(count);
-
var ary = [0,1,2]; ary[10] = 10; ary.filter(function(x) { return x === undefined;});
-
var two = 0.2 var one = 0.1 var eight = 0.8 var six = 0.6 [two - one == one, eight - six == two]
-
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'));
-
function 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!'); } } showCase(String('A'));
-
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);
-
parseInt(3, 8) parseInt(3, 2) parseInt(3, 0)
-
Array.isArray( Array.prototype )
-
var a = [0]; if ([0]) { console.log(a == true); } else { console.log("wut"); }
-
[]==[]
-
'5' + 3 '5' - 3
-
1 + - + + + - + 1
-
var ary = Array(3); ary[0]=2 ary.map(function(elem) { return '1'; });
-
function sidEffecting(ary) { ary[0] = ary[2]; } function bar(a,b,c) { c = 10 sidEffecting(arguments); return a + b + c; } bar(1,1,1)
-
var a = 111111111111111110000, b = 1111; a + b;
-
Number.MIN_VALUE > 0
-
[1 < 2 < 3, 3 < 2 < 1]
-
2 == [[[2]]]
-
3.toString() 3..toString() 3...toString()
-
(function(){ var x = y = 1; })(); console.log(y); console.log(x);
-
var a = /123/, b = /123/; a == b a === b
-
var a = [1, 2, 3], b = [1, 2, 3], c = [1, 2, 4] a == b a === b a > c a < c
-
var a = {}, b = Object.prototype; [a.prototype === b, Object.getPrototypeOf(a) === b]
-
function f() {} var a = f.prototype, b = Object.getPrototypeOf(f); a === b
-
function foo() { } var oldName = foo.name; foo.name = "bar"; [oldName, foo.name]
-
"1 2 3".replace(/\d/g, parseInt)
-
function f() {} var parent = Object.getPrototypeOf(f); f.name // ? parent.name // ? typeof eval(f.name) // ? typeof eval(parent.name) // ?
-
var lowerCaseOnly = /^[a-z]+$/; [lowerCaseOnly.test(null), lowerCaseOnly.test()]
-
[,,,].join(", ")
-
var a = {class: "Animal", name: 'Fido'}; a.class