10个 JavaScript 变态题解析 ——动力节点java
来自: 丑态
今天有参加面试的同学,发给了动宝儿10个超级变态面试题,说是让我也感受一下这个酸爽,动宝儿想,独乐乐不如众乐乐,分享给大家感受一下。 第1题 var a = 111111111111111110000, b = 1111; a + b; 答案还是111111111111111110000. 解释是Lack of precision for numbers in JavaScript affects both small and big numbers.但是笔者不是很明白……………. 请读者赐教! 第2题 var x = [].reverse; x(); 这个题有意思! 知识点: Array/reverse 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 第3题 Number.MIN_VALUE > 0 true 第4题 [1 < 2 < 3, 3 < 2 < 1] 这个题也还可以. 这个题会让人误以为是2 > 1 && 2 < 3其实不是的. 这个题等价于 1 < 2 => true; true < 3 => 1 < 3 => true; 3 < 2 => false; false < 1 => 0 < 1 => true; 答案是[true, true] 第5题 // the most classic wtf 2 == [[[2]]] 这个题我是猜的. 我猜的true, 至于为什么….. both objects get converted to strings and in both cases the resulting string is "2"我不能信服… 第6题 3.toString() 3..toString() 3...toString() 这个题也挺逗, 我做对了答案是error, '3', error 你如果换一个写法就更费解了 var a = 3; a.toString() 这个答案就是'3'; 为啥呢? 因为在 js 中1.1,1.,.1都是合法的数字. 那么在解析3.toString的时候这个.到底是属于这个数字还是函数调用呢? 只能是数字, 因为3.合法啊! 第7题 (function(){ var x = y = 1; })(); console.log(y); console.log(x); 答案是1, error y 被赋值到全局. x 是局部变量. 所以打印 x 的时候会报ReferenceError 第8题 var a = /123/, b = /123/; a == b a === b 即使正则的字面量一致, 他们也不相等. 答案false, false 第9题 var a = [1, 2, 3], b = [1, 2, 3], c = [1, 2, 4] a == b a === b a > c a < c 字面量相等的数组也不相等. 数组在比较大小的时候按照字典序比较 答案false, false, false, true 第10题 var a = {}, b = Object.prototype; [a.prototype === b, Object.getPrototypeOf(a) === b] 知识点: Object/getPrototypeOf 只有 Function 拥有一个 prototype 的属性. 所以a.prototype为undefined. 而Object.getPrototypeOf(obj)返回一个具体对象的原型(该对象的内部[[prototype]]值) 答案false, true 文章来源:http://www.bjpowernode.com/hyzx/20160906-434.html
-
逆水行舟 赞了这篇讨论 2017-11-12 23:01:51
最新讨论 ( 更多 )
- SpringBoot2 仿B站高性能前端+后端项目[完结16章]下载 (huamdns1990)
- Java自学团,匹配学习伙伴,一起打卡学习,做实战项目 (饭团🍙)
- 【完结10章】SpringBoot 3 + Flutter3 实战低代码运营管理 (克里斯蒂安)
- 基于SpringBoot3.x+Vue3.x整合从0到1一步一步实现酒店管理系统 (噜啦噜啦嘞)
- [升级16章+电子书]SpringBoot+Vue3 项目实战,打造企业级在线... (快乐至上2022)