## 第1题

``````["1", "2", "3"].map(parseInt)
``````

``````parseInt('1', 0);
parseInt('2', 1);
parseInt('3', 2);
``````

## 第2题

``````[typeof null, null instanceof Object]
``````

typeof 返回一个表示类型的字符串.

instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上.

typeof 的结果请看下表:

``````type         result
Undefined   "undefined"
Null        "object"
Boolean     "boolean"
Number      "number"
String      "string"
Symbol      "symbol"
Host object Implementation-dependent
Function    "function"
Object      "object"
``````

## 第3题

``````[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
``````

`arr.reduce(callback[, initialValue])`

reduce接受两个参数, 一个回调, 一个初始值.

## 第4题

``````var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
``````

## 第5题

``````var name = 'World!';
(function () {
if (typeof name === 'undefined') {
var name = 'Jack';
console.log('Goodbye ' + name);
} else {
console.log('Hello ' + 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);
``````

## 第7题

``````var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;});
``````

``````if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';

if (this === void 0 || this === null) {
throw new TypeError();
}

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}

var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) { // 注意这里!!!
var val = t[i];
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}

return res;
};
}
``````

``````0 in ary; => true
3 in ary; => false
10 in ary; => true
``````

## 第8题

``````var two   = 0.2
var one   = 0.1
var eight = 0.8
var six   = 0.6
[two - one == one, eight - six == two]
``````

IEEE 754标准中的浮点数并不能精确地表达小数

## 第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'));
``````

switch 是严格比较, String 实例和 字符串不一样.

``````var s_prim = 'foo';
var s_obj = new String(s_prim);

console.log(typeof s_prim); // "string"
console.log(typeof s_obj);  // "object"
console.log(s_prim === s_obj); // false

``````

## 第10题

``````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!');
}
}
showCase2(String('A'));
``````

`String(x) does not create an object but does return a string, i.e. typeof String(1) === "string"`

## 第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);
``````

``````7 % 2 => 1
4 % 2 => 0
'13' % 2 => 1
-9 % % 2 => -1
Infinity % 2 => NaN
``````

## 第12题

``````parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)
``````

## 第13题

``````Array.isArray( Array.prototype )
``````

## 第14题

``````var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log("wut");
}
``````

## 第15题

``````[]==[]

``````

`==` 是万恶之源, 看上图

## 第16题

``````'5' + 3
'5' - 3
``````

`+` 用来表示两个数的和或者字符串拼接, `-`表示两数之差.

``````> '5' + 3
'53'
> 5 + '3'
'53'
> 5 - '3'
2
> '5' - 3
2
> '5' - '3'
2
``````

## 第17题

``````1 + - + + + - + 1

``````

``````1 + (a)  => 2
a = - (b) => 1
b = + (c) => -1
c = + (d) => -1
d = + (e) => -1
e = + (f) => -1
f = - (g) => -1
g = + 1   => 1
``````

## 第18题

``````var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return '1'; });
``````

``````Array.prototype.map = function(callback, thisArg) {

var T, A, k;

if (this == null) {
throw new TypeError(' this is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = thisArg;
}
A = new Array(len);
k = 0;
while (k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}
return A;
};
``````

## 第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)

``````

``````function sidEffecting(ary) {
ary[0] = ary[2];
}
function bar(a,b,c=3) {
c = 10
sidEffecting(arguments);
return a + b + c;
}
bar(1,1,1)
``````

## 第20题

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

``````

## 第21题

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

`The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.`

## 第22题

``````Number.MIN_VALUE > 0
``````

`true`

## 第23题

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

`````` 1 < 2 => true;
true < 3 =>  1 < 3 => true;
3 < 2 => false;
false < 1 => 0 < 1 => true;
``````

## 第24题

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

`both objects get converted to strings and in both cases the resulting string is "2"` 我不能信服...

## 第25题

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

``````var a = 3;
a.toString()
``````

## 第26题

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

y 被赋值到全局. x 是局部变量. 所以打印 x 的时候会报 `ReferenceError`

## 第27题

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

## 第28题

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

## 第29题

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

## 第30题

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

f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy.

f.prototype 是使用使用 new 创建的 f 实例的原型. 而 Object.getPrototypeOf 是 f 函数的原型.

``````
a === Object.getPrototypeOf(new f()) // true
b === Function.prototype // true
``````

## 31

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

## 第32题

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

`str.replace(regexp|substr, newSubStr|function)`

• match 首先是匹配的字符串
• p1, p2 .... 然后是正则的分组
• offset match 匹配的index
• string 整个字符串

``````parseInt('1', 0)
parseInt('2', 2)
parseInt('3', 4)
``````

## 第33题

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

## 第34题

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

## 第35题

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

`[,,,] => [undefined × 3]`

## 第36题

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

## 第37题

``````var a = new Date("epoch")
``````

## 第38题

``````var a = Function.length,
b = new Function().length
a === b
``````

## 第39题

``````var a = Date(0);
var b = new Date(0);
var c = new Date();
[a === b, b === c, a === c]
``````

• 如果不传参数等价于当前时间.
• 如果是函数调用 返回一个字符串.

## 第40题

``````var min = Math.min(), max = Math.max()
min < max
``````

## 第41题

``````function captureOne(re, str) {
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]
``````

``````var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
// Found abb. Next match starts at 3
// Found ab. Next match starts at 9
``````

## 第42题

``````var a = new Date("2014-03-19"),
b = new Date(2014, 03, 19);
[a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]

``````

JavaScript inherits 40 years old design from C: days are 1-indexed in C's struct tm, but months are 0 indexed. In addition to that, getDay returns the 0-indexed day of the week, to get the 1-indexed day of the month you have to use getDate, which doesn't return a Date object.

``````a.getDay()
3
b.getDay()
6
a.getMonth()
2
b.getMonth()
3
``````

## 第43题

``````if ('http://giftwrapped.com/picture.jpg'.match('.gif')) {
'a gif file'
} else {
'not a gif file'
}
``````

String.prototype.match 接受一个正则, 如果不是, 按照 `new RegExp(obj)` 转化. 所以 `.` 并不会转义

## 第44题

``````function foo(a) {
var a;
return a;
}
function bar(a) {
var a = 'bye';
return a;
}
[foo('hello'), bar('hello')]
``````

# 总结

## 44个 Javascript 变态题解析 (上\下)的更多相关文章

1. 44个 Javascript 变态题解析 (上)

原题来自: javascript-puzzlers(http://javascript-puzzlers.herokuapp.com/) 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… ...

2. 44个 Javascript 变态题解析 (下)

承接上篇 44个 Javascript 变态题解析 (上) 第23题 [1 < 2 < 3, 3 < 2 < 1] 这个题也还可以. 这个题会让人误以为是 2 > 1 & ...

3. 44 个 JavaScript 变态题解析

当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了…. 不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变态题到底变态不变态吧! 第1题 ["1", &qu ...

4. 44个javascript 变态题解析

原题来自: javascript-puzzlers 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了…. 不过, 对于基础知识的理 ...

5. 44个 Javascript 变态题解析

原题来自: http://javascript-puzzlers.herokuapp.com/ 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44... 当初笔者做这套题的时候不仅怀疑智商, ...

6. 44个 Javascript 变态题解析——分分钟让你怀疑人生

原题来自: http://javascript-puzzlers.herokuapp.com/ 第1题 ["1", "2", "3"].ma ...

7. JS夯实基础：Javascript 变态题解析 (上)

["].map(parseInt) 1.知识点: Array/map Number/parseInt Global_Objects/parseInt JavaScript parseInt ...

8. Javascript 变态题解析

读者可以先去做一下感受感受. 当初笔者的成绩是 21/44... 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了.... 不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变 ...

9. JS夯实基础：Javascript 变态题解析 (下)

function sidEffecting(ary) { ary[] = ary[]; } function bar(a,b,c) { c = sidEffecting(arguments); ret ...

## 随机推荐

1. JavaSE高级之GUI编程

下面主要用到了java中的swing进行界面设计,当然java的GUI不如C#的设计的好看,不过原理还是要会的. 1． GUI Graphical User Interface 用户图形界面 a) 主 ...

2. js兼容性问题

javscript 浏览器兼容性问题: scrollTop = document.documentElement.scrollTop || document.body.scrollTop; scrol ...

3. poj 3358 Period of an Infinite Binary Expansion

由乘2取整得到分数的小数位,可以找到规律!!! 例如:1/10,2/10,4/10,8/10,16/10,32/10,64/10…… 取整后:1/10,2/10,4/10,8/10,6/10,2/10 ...

4. 车牌识别LPR（七）-- 字符特征

第七篇:字符特征 选择的字符特征应该满足以下条件: (1)选取的字符特征具有较强的鲁棒性,不受字符变形.弯曲等影响. (2)两个字符的字符特征不能完全相同,但部分相同是允许的,即选择的字符特征是唯一的 ...

5. 动态规划（树形DP）：LNOI 2016 侦察守卫

Sample Input 12 2 8 9 12 6 1 1 5 1 4 8 10 6 10 1 2 3 5 6 7 8 9 10 11 1 3 2 3 3 4 4 5 4 6 4 7 7 8 8 9 ...

6. openstack私有云布署实践【3 keepalive配置（VRRP虚IP）】

这里只用到keepalived 或者pacemaker两种方法,可二选一来配置,官方提供的是pacemaker,但我们实际测试环境和生产环境上使的是keepalive     首先在4台control ...

7. c# socket传输struct类型

data结构体类型 public struct datas    { public string test1; public string test2;    } //socket服务器端 publi ...

8. 实际开发中，实用的辅助iOS开发的工具

就目前所知,开发iOS绝大部分都是用Xcode,除此工具之外,还有几个好用的可以辅助实际开发中遇到的问题,拥有这种辅助开发技能,在工作中,甚至是以后的面试中,都可能会有不小的帮助. 下面推荐三个实用的 ...

9. ThreeJS笔记(一)

ThreeJS笔记(一) ThreeJS的本质是WebGL,但是webGL使用起来不是很方便,threeJS则提供了比较上层的封装,开发起来更加轻便. 环境配置 首先从github下载源码 把Thre ...

10. Linux Doxygen的安装和使用