44个javascript 变态题解析
原题来自: javascript-puzzlers
读者可以先去做一下感受感受. 当初笔者的成绩是 21/44…
当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了….
不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变态题到底变态不变态吧!
第1题
1 |
["1", "2", "3"].map(parseInt) |
知识点:
首先, map接受两个参数, 一个回调函数 callback, 一个回调函数的this值
其中回调函数接受三个参数 currentValue, index, arrary;
而题目中, map只传入了回调函数–parseInt.
其次, parseInt 只接受两个两个参数 string, radix(基数).
在没有指定基数,或者基数为 0 的情况下,JavaScript 作如下处理:
- 如果字符串 string 以”0x”或者”0X”开头, 则基数是16 (16进制).
- 如果字符串 string 以”0”开头, 基数是8(八进制)或者10(十进制),那么具体是哪个基数由实现环境决- 定。ECMAScript 5 规定使用10,但是并不是所有的浏览器都遵循这个规定。因此,永远都要明确给出radix参数的值。
- 如果字符串 string 以其它任何值开头,则基数是10 (十进制)。
所以本题即问
1 |
parseInt('1', 0); |
首先后两者参数不合法.
所以答案是 [1, NaN, NaN]
第2题
1 |
[typeof null, null instanceof Object] |
两个知识点:
typeof 返回一个表示类型的字符串.
instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上.
这个题可以直接看链接… 因为 typeof null === 'object'
自语言之初就是这样….
typeof 的结果请看下表:
1 |
type result |
所以答案 [object, false]
第3题
1 |
[ [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题
1 |
var val = 'smtg'; |
两个知识点:
简而言之 +
的优先级 大于 ?
所以原题等价于 'Value is true' ? 'Somthing' : 'Nonthing'
而不是 'Value is' + (true ? 'Something' : 'Nonthing')
答案 'Something'
第5题
1 |
var name = 'World!'; |
这个相对简单, 一个知识点:
在 JavaScript中, functions 和 variables 会被提升。变量提升是JavaScript将声明移至作用域 scope (全局域或者当前函数作用域) 顶部的行为。
这个题目相当于
1 |
var name = 'World!'; |
所以答案是 'Goodbye Jack'
第6题
1 |
var END = Math.pow(2, 53); |
一个知识点:
在 JS 里, Math.pow(2, 53) == 9007199254740992 是可以表示的最大值. 最大值加一还是最大值. 所以循环不会停.
补充: @jelly7723
js中可以表示的最大整数不是2的53次方,而是1.7976931348623157e+308。
2的53次方不是js能表示的最大整数而应该是能正确计算且不失精度的最大整数,可以参见js权威指南。
9007199254740992 +1还是 9007199254740992 ,这就是因为精度问题,如果 9007199254740992 +11或者 9007199254740992 +111的话,值是会发生改变的,只是这时候计算的结果不是正确的值,就是因为精度丢失的问题。
第7题
1 |
var ary = [0,1,2]; |
答案是 []
看一篇文章理解稀疏数组
我们来看一下 Array.prototype.filter 的 polyfill:
1 |
if (!Array.prototype.filter) { |
我们看到在迭代这个数组的时候, 首先检查了这个索引值是不是数组的一个属性, 那么我们测试一下.
1 |
0 in ary; => true |
也就是说 从 3 - 9 都是没有初始化的’坑’!, 这些索引并不存在与数组中. 在 array 的函数调用的时候是会跳过这些’坑’的.
第8题
1 |
var two = 0.2 |
IEEE 754标准中的浮点数并不能精确地表达小数
那什么时候精准, 什么时候不经准呢? 笔者也不知道…
答案 [true, false]
第9题
1 |
function showCase(value) { |
两个知识点:
switch 是严格比较, String 实例和 字符串不一样.
1 |
var s_prim = 'foo'; |
答案是 'Do not know!'
第10题
1 |
function showCase2(value) { |
解释:String(x) does not create an object but does return a string, i.e. typeof String(1) === "string"
还是刚才的知识点, 只不过 String 不仅是个构造函数 直接调用返回一个字符串哦.
答案 'Case A'
第11题
1 |
function isOdd(num) { |
一个知识点
此题等价于
1 |
7 % 2 => 1 |
需要注意的是 余数的正负号随第一个操作数.
答案 [true, true, true, false, false]
第12题
1 |
parseInt(3, 8) |
第一个题讲过了, 答案 3, NaN, 3
第13题
1 |
Array.isArray( Array.prototype ) |
一个知识点:
一个鲜为人知的实事: Array.prototype => []
;
答案: true
第14题
1 |
var a = [0]; |
- JavaScript-Equality-Table
- _更新_通过一张简单的图,让你彻底地、永久地搞懂JS的==运算 非常不错的一篇文章!
解析:
Boolean([0]) === true
- [0] == true
- true 转换为数字 => 1
- [0] 转化为数字失败, 转化为字符串 ‘0’, 转化成数字 => 0
- 0 !== 1
答案: false
第15题
1 |
[]==[] |
[] 是Object, 两个 Object 不相等
答案是 false
第16题
1 |
'5' + 3 |
两个知识点:
+
用来表示两个数的和或者字符串拼接, -
表示两数之差.
请看例子, 体会区别:
1 |
> '5' + 3 |
也就是说 -
会尽可能的将两个操作数变成数字, 而 +
如果两边不都是数字, 那么就是字符串拼接.
答案是 '53', 2
第17题
1 |
1 + - + + + - + 1 |
这里应该是(倒着看)
1 |
1 + (a) => 2 |
所以答案 2
第18题
1 |
var ary = Array(3); |
稀疏数组. 同第7题.
题目中的数组其实是一个长度为3, 但是没有内容的数组, array 上的操作会跳过这些未初始化的’坑’.
所以答案是 ["1", undefined × 2]
这里贴上 Array.prototype.map 的 polyfill.
1 |
Array.prototype.map = function(callback, thisArg) { var T, A, k; if (this == null) { |
第19题
1 |
function sidEffecting(ary) { |
这是一个大坑, 尤其是涉及到 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
了…..
请看:
1 |
function sidEffecting(ary) { |
答案是 12
!!!!
请读者细细体会!!
第20题
1 |
var a = 111111111111111110000, |
答案还是 111111111111111110000
. 解释是 Lack of precision for numbers in JavaScript affects both small and big numbers.
但是笔者不是很明白……………. 请读者赐教!
第21题
1 |
var x = [].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
.
补充:
@stellar91 这个笔者实践了一下 发现 firefox 是 window, chrome 报错
VM190:2 Uncaught TypeError: Array.prototype.reverse called on null or undefined(…)
可能是实现不同, 在 chrome 中应该是对调用者做了检查.
答案是 window
第22题
1 |
Number.MIN_VALUE > 0 |
true
@10081677wc
MIN_VALUE 属性是 JavaScript 中可表示的最小的数(接近 0 ,但不是负数),它的近似值为 5 x 10-324。
第23题
1 |
[1 < 2 < 3, 3 < 2 < 1] |
这个题也还可以.
这个题会让人误以为是 2 > 1 && 2 < 3
其实不是的.
这个题等价于
1 |
1 < 2 => true; |
答案是 [true, true]
第24题
1 |
// the most classic wtf |
这个题我是猜的. 我猜的 true
, 至于为什么…..
both objects get converted to strings and in both cases the resulting string is "2"
我不能信服…
第25题
1 |
3.toString() |
这个题也挺逗, 我做对了 :) 答案是 error, '3', error
你如果换一个写法就更费解了
1 |
var a = 3; |
这个答案就是 '3'
;
为啥呢?
因为在 js 中 1.1
, 1.
, .1
都是合法的数字. 那么在解析 3.toString
的时候这个 .
到底是属于这个数字还是函数调用呢? 只能是数字, 因为3.
合法啊!
第26题
1 |
(function(){ |
答案是 1, error
y 被赋值到全局. x 是局部变量. 所以打印 x 的时候会报 ReferenceError
第27题
1 |
var a = /123/, |
即使正则的字面量一致, 他们也不相等.
答案 false, false
第28题
1 |
var a = [1, 2, 3], |
字面量相等的数组也不相等.
数组在比较大小的时候按照字典序比较
答案 false, false, false, true
第29题
1 |
var a = {}, b = Object.prototype; |
知识点:
只有 Function 拥有一个 prototype 的属性. 所以 a.prototype
为 undefined
.
而 Object.getPrototypeOf(obj)
返回一个具体对象的原型(该对象的内部[[prototype]]
值)
答案 false, true
第30题
1 |
function f() {} |
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 函数的原型.
请看:
1 |
a === Object.getPrototypeOf(new f()) // true |
答案 false
第31题
1 |
function foo() { } |
答案 ['foo', 'foo']
知识点:
因为函数的名字不可变.
第32题
1 |
"1 2 3".replace(/\d/g, parseInt) |
知识点:
str.replace(regexp|substr, newSubStr|function)
如果replace函数传入的第二个参数是函数, 那么这个函数将接受如下参数
- match 首先是匹配的字符串
- p1, p2 …. 然后是正则的分组
- offset match 匹配的index
- string 整个字符串
由于题目中的正则没有分组, 所以等价于问
1 |
parseInt('1', 0) |
答案: 1, NaN, 3
第33题
1 |
function f() {} |
先说以下答案 'f', 'Empty', 'function', error
这个答案并不重要…..
这里第一小问和第三小问很简单不解释了.
第二小问笔者在自己的浏览器测试的时候是 ''
, 第四问是 'undefined'
所以应该是平台相关的. 这里明白 parent === Function.prototype
就好了.
第34题
1 |
var lowerCaseOnly = /^[a-z]+$/; |
知识点:
这里 test 函数会将参数转为字符串. 'nul'
, 'undefined'
自然都是全小写了
答案: true, true
第35题
1 |
[,,,].join(", ") |
[,,,] => [undefined × 3]
因为javascript 在定义数组的时候允许最后一个元素后跟一个,
, 所以这是个长度为三的稀疏数组(这是长度为三, 并没有 0, 1, 2三个属性哦)
答案: ", , "
第36题
1 |
var a = {class: "Animal", name: 'Fido'}; |
这个题比较流氓.. 因为是浏览器相关, class
是个保留字(现在是个关键字了)
所以答案不重要, 重要的是自己在取属性名称的时候尽量避免保留字. 如果使用的话请加引号 a['class']
第37题
1 |
var a = new Date("epoch") |
知识点:
简单来说, 如果调用 Date 的构造函数传入一个字符串的话需要符合规范, 即满足 Date.parse 的条件.
另外需要注意的是 如果格式错误 构造函数返回的仍是一个Date 的实例 Invalid Date
.
答案 Invalid Date
第38题
1 |
var a = Function.length, |
我们知道一个function(Function 的实例)的 length
属性就是函数签名的参数个数, 所以 b.length == 0.
另外 Function.length 定义为1……
所以不相等…….答案 false
第39题
1 |
var a = Date(0); |
还是关于Date 的题, 需要注意的是
- 如果不传参数等价于当前时间.
- 如果是函数调用 返回一个字符串.
答案 false, false, false
第40题
1 |
var min = Math.min(), max = Math.max() |
知识点:
有趣的是, Math.min 不传参数返回 Infinity
, Math.max 不传参数返回 -Infinity
最新文章
- Spring Security 3整合CAS 实现SSO
- Ajax从服务器端获取数据
- Visual Studio中编写C程序
- 22 扩展Python - 《Python 核心编程》
- Eclipse无法进入Debug模式
- [jobdu]第一个只出现一次的字符
- Activity的启动过程
- Web 应用性能和压力测试工具 Gor - 运维生存时间
- Java常见序列化与反序列方法总结
- HDU 4712Hamming Distance(随机函数运用)
- hdu 亲和串(kmp)
- CodeForces 681D Gifts by the List
- 笔记一:OOAD与UML
- Bzoj2442:修剪草坪
- iptables 配置说明
- long long 的输入输出问题
- Qt ------ QDir 路径
- 56.关于vue项目的seo问题
- tomcat7.0安装笔记
- max10之pll时钟源切换