# 前端经典面试题 不经典不要star！

## 正文

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

//第2题
[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]

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

//第4题
[typeof null, null instanceof Object]

//第5题
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 END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
count++;
}
console.log(count);

``````

## 接下来一道一道咱们去慢慢解析

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

1. Array/map
2. Number/parseInt
3. JavaScript parseInt

``````首先, map接受两个参数, 一个回调函数 callback, 一个回调函数的this值

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

如果研究理解了
parseInt(3, 8)
parseInt(3, 2)    //下方评论该题答案  别作弊哦！
parseInt(3, 0)

``````

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

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

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

``````

``````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``````

``[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"
``````

``````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)``````

6.5k 声望
1.2k 粉丝