44个 Javascript 变态题剖析 (上)

原文来自我的 github

原题来自: javascript-puzzlers

44个 Javascript 变态题剖析 (下)

读者能够先去做一下感觉感觉. 当初笔者的效果是 21/44…

当初笔者做这套题的时刻不仅疑心智商, 连人生都最先疑心了….

不过, 关于基础知识的邃晓是深切编程的条件. 让我们一起来看看这些变态题究竟变态不变态吧!

第1题

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

知识点:

起首, map接收两个参数, 一个回调函数 callback, 一个回调函数的this值

个中回调函数接收三个参数 currentValue, index, arrary;

而问题中, map只传入了回调函数–parseInt.

其次, parseInt 只接收两个两个参数 string, radix(基数). radix的正当区间是2-36. 默许是10.

所以本题即问

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

起首后二者参数不正当. 第一个笔者猜想0和不传一样被认为是10.

所以答案是 [1, NaN, NaN]

第2题

[typeof null, null instanceof Object]

两个知识点:

typeof 返回一个示意范例的字符串.

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

这个题能够直接看链接… 由于 typeof null === 'object' 自语言之初就是如许….

typeof 的效果请看下表:

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

所以答案 [object, false]

第3题

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

知识点:

arr.reduce(callback[, initialValue])

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

回调函数接收四个参数 previousValue, currentValue, currentIndex, array

须要注重的是 If the array is empty and no initialValue was provided, TypeError would be thrown.

所以第二个表达式会报非常. 第一个表达式等价于 Math.pow(3, 2) => 9; Math.pow(9, 1) =>9

答案 an error

第4题

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

两个知识点:

简而言之 + 的优先级 大于 ?

所以原题等价于 'Value is true' ? 'Somthing' : 'Nonthing' 而不是 'Value is' + (true ? 'Something' : 'Nonthing')

答案 'Something'

第5题

var name = 'World!';
(function () {
    if (typeof name === 'undefined') {
        var name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();

这个相对简朴, 一个知识点:

在 JavaScript中, functions 和 variables 会被提拔。变量提拔是JavaScript将声明移至作用域 scope (全局域或许当前函数作用域) 顶部的行动。

这个问题相当于

var name = 'World!';
(function () {
    var name;
    if (typeof name === 'undefined') {
        name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();

所以答案是 'Goodbye Jack'

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

一个知识点:

在 JS 里, Math.pow(2, 53) == 9007199254740992 是能够示意的最大值. 最大值加一照样最大值. 所以轮回不会停.

第7题

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

答案是 []

看一篇文章邃晓希罕数组

我们来看一下 Array.prototype.filter 的 polyfill:

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

也就是说 从 3 – 9 都是没有初始化的’坑’!, 这些索引并不存在与数组中. 在 array 的函数挪用的时刻是会跳过这些’坑’的.

第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规范中的浮点数并不能精确地表达小数

那什么时刻精准, 什么时刻不经准呢? 笔者也不知道…

答案 [true, false]

第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

答案是 'Do not know!'

第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"

照样适才的知识点, 只不过 String 不仅是个组织函数 直接挪用返回一个字符串哦.

答案 'Case A'

第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

须要注重的是 余数的正负号随第一个操纵数.

答案 [true, true, true, false, false]

第12题

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

第一个题讲过了, 答案 3, NaN, 3

第13题

Array.isArray( Array.prototype )

一个知识点:

一个不为人知的实事: Array.prototype => [];

答案: true

第14题

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

一图胜千言

《44个 Javascript 变态题剖析 (上)》
《44个 Javascript 变态题剖析 (上)》

答案: false

第15题

[]==[]

== 是万恶之源, 看上图

答案是 false

第16题

'5' + 3
'5' - 3

两个知识点:

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

请看例子, 体味区分:

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

也就是说 - 会尽能够的将两个操纵数变成数字, 而 + 假如双方不都是数字, 那末就是字符串拼接.

答案是 '53', 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

所以答案 2

第18题

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

希罕数组. 同第7题.

问题中的数组实际上是一个长度为3, 然则没有内容的数组, array 上的操纵会跳过这些未初始化的’坑’.

所以答案是 ["1", undefined × 2]

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

这是一个大坑, 尤其是涉及到 ES6语法的时刻

知识点:

起首 The arguments object is an Array-like object corresponding to the arguments passed to a function.

也就是说 arguments 是一个 object, c 就是 arguments[2], 所以关于 c 的修正就是对 arguments[2] 的修正.

所以答案是 21.

但是!!!!!!

当函数参数涉及到 any rest parameters, any default parameters or any destructured parameters 的时刻, 这个 arguments 就不在是一个 mapped arguments object 了…..

请看:

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)

答案是 12 !!!!

请读者细细体味!!

第20题


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

答案照样 111111111111111110000. 诠释是 Lack of precision for numbers in JavaScript affects both small and big numbers. 然则笔者不是很邃晓……………. 请读者见教!

精读者提醒 是凌驾浮点数的精度了, 笔者没有仔细看规范…

第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.

也就是说 末了会返回这个挪用者(this), 但是 x 实行的时刻是上下文是全局. 那末末了返回的是 window.

答案是 window

第22题

Number.MIN_VALUE > 0

true

本日先到这里, 下次我们来看后22个题!

44个 Javascript 变态题剖析 (下)

    原文作者:xiaoyu2er
    原文地址: https://segmentfault.com/a/1190000005681454
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞