一组JavaScript试题(包含一些容易混淆的知识点)

来源:互联网 发布:大斧头软件 编辑:程序博客网 时间:2024/05/06 20:27

今天刷知乎的时候无意间发现的一组JavaScript试题,做了一下...共35道,对14,错17,十分惨痛,在这里整理一下这些题目,梳理一下其中的知识点

JavaScript专业八级测试,你能做对几道?

1.

> ["1", "2", "3"].map(parseInt)[ 1, NaN, NaN ]
这里涉及到的知识点是parseInt函数

parseInt函数接受两个参数分别为String和radix

第一个参数代表被解析的字符串,第二个参数表示的是要解析的数字的基数,是一个2~36之间的数

注意 如果radix写作0或者省略的话,js解释器会把他当做10来处理,若参数小于2或者大于36,则返回NaN

再来看这个题目,map传递过来的是三个参数(element, index, array),然而parseInt是只能接受两个参数的

这也就意味着代码执行了这几个步骤

> parseInt("3", 2)NaN> parseInt("1", 0)1> parseInt("2",1)NaN> parseInt("3", 2)NaN

2.
> [typeof null, null instanceof Object][ 'object', false ]
typeof对原生非可调用对象会始终返回Object,这里null被认为是一个空的对象引用

3.

[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)] ]
在一个空数组上执行reduce函数会跑出初始化错误的异常TypeError

4.

> var val = 'smtg';console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');Something
这里涉及到js的运算符优先级,考虑括号中的表达式,得出运算顺序为() , + , ?:

5.

>     (function () {...       if (typeof name === 'undefined') {.....         var name = 'Jack';.....         console.log('Goodbye ' + name);.....       } else {.....         console.log('Hello ' + name);.....       }...     })();Goodbye Jack
变量提升,js解释器会在执行时自动将name的声明提升到if语句之前,此时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);
代码进入死循环,2^53是JavaScript中最大的数,i怎么样也不会比这个数还大,无法跳出循环

7.

>ary.filter(function(x) { return x === undefined;});[]
Array.prototype.filter不会应用到缺失的元素上

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 ]
检测一下

> 0.8 - 0.60.20000000000000007
JavaScript中没有精准的数字,浮点数值的最高精度是17位小数,但在进行算数运算时精度远远不及整数,因此,永远不要测试某个特定的浮点数值

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'));
output: Do not know!

首先要清楚的是switch是用===来枚举的,而new String('A')实际上是调用String构造函数创建了一个对象

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!');      }    }    showCase(String('A'));
output: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);[ true, true, true, false, false ]
-9 / 2 = -1; Infinity % 2 = NaN

12.

>     parseInt(3, 8)3>     parseInt(3, 2)NaN>     parseInt(3, 0)3
0会被当做10来处理

13.

> Array.isArray( Array.prototype )true
14.
var a = [0];    if ([0]) {       console.log(a == true);    } else {       console.log("wut");    }false
首先可以看一下下面两个语句

> Boolean([0])true> [0] == truefalse
可见,[0]被当做true却又不等同于true

15.

> [] == []false
这两个[]代表不同的对象

16.

> '5' + 3'53'> '5' - 32

String会处理加法,这里将3转化为字符串

遇到减法的话则把字符串转化为数字

可以结合一下两个语句理解一下

> 1 + 2 + 3 + '1234567''61234567'> '5' + 3 + 4 + '222''534222'

17.
> 1 + - + + + - + 12
1+后面的运算符全部被当做后面的1的正负号来处理

修改一下 出现以下的现象

> 1 + - + + + - - 10
18.
var ary = Array(3);ary[0]=2ary.map(function(elem) { return '1'; });    ["1", undefined × 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
arguments为[1,1,10]

20.

  bar(1,1,1)21var a = 111111111111111110000,b = 1111;a + b;    111111111111111110000
不精确的JavaScript会影响小数,也会影响大数

21.

> Number.MIN_VALUE > 0true
MIN_VALUE表示的是最小的比零大的数

22.

> [1 < 2 < 3, 3 < 2 < 1][ true, true ]
隐式转换 true < 3    false < 1

23.

> 2 == [[[2]]]true
对象都被转换为String,最后变为‘2’

24.

> 3..toString()'3'
相当于3.0.toString()

25.

(function(){      var x = y = 1;    })();console.log(y);console.log(x);1, error
(function(){})()模仿了块级作用域,所以x是访问不到的

26.

var a = /123/, b = /123/;    a == b    a === bfalse
每个正则表达式都是唯一的

27.

> var a = [1, 2, 3],...      b = [1, 2, 3],...      c = [1, 2, 4]>     a ==  bfalse>     a === bfalse>     a > cfalse>     a < ctrue
数组通过> <按顺序比较但是== ===不会

28.

var a = {}, b = Object.prototype;[a.prototype === b, Object.getPrototypeOf(a) === b][false, true]
Object.prototype = {} 而a.prototype = undefined

29.

function f() {}var a = f.prototype, b = Object.getPrototypeOf(f);a === bfalse
30.
function foo() { }var oldName = foo.name;foo.name = "bar";[oldName, foo.name]["foo", "foo"]
31.
</pre><pre name="code" class="javascript">"1 2 3".replace(/\d/g, parseInt)"1 NaN 3"
考虑parseInt的参数,见第一道题目

32.

> function f() {}undefined>     var parent = Object.getPrototypeOf(f);undefined>     f.name // ?'f'>     parent.name // ?''>     typeof eval(f.name) // ?'function'>     typeof eval(parent.name) //  ?'undefined'
33.
var lowerCaseOnly =  /^[a-z]+$/;[lowerCaseOnly.test(null), lowerCaseOnly.test()][true, true]
test()中的参数会被 转化为字符串

34.

> [,,,].join(", ")', , '
实际上原数组相当于存了三个undefined,最后多了一个逗号,可能这样比较容易理解 var sss = [1,,2,3,,];最后一个逗号不影响数组元素

35.

 var a = {class: "Animal", name: 'Fido'}; a.class
有的浏览器中class是保留字,不可作为属性名出现

0 0
原创粉丝点击