44個javascript 變態題解析
原題來自: javascript-puzzlers
讀者可以先去做一下感受感受. 當初筆者的成績是 21/44…
當初筆者做這套題的時候不僅懷疑智商, 連人生都開始懷疑了….
不過, 對於基礎知識的理解是深入編程的前提. 讓我們一起來看看這些變態題到底變態不變態吧!
第1題
1
|
["1", "2", "3"].map(parseInt)
|
知識點:
- Array/map
- Number/parseInt
- Global_Objects/parseInt
- JavaScript 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]
|
兩個知識點:
- Operators/typeof
- Operators/instanceof
- Operators/instanceof(中)
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) ]
|
知識點:
- Array/Reduce
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‘;
|
兩個知識點:
- Operators/Operator_Precedence
- Operators/Conditional_Operator
簡而言之 +
的優先級 大於 ?
所以原題等價於 ‘Value is true‘ ? ‘Somthing‘ : ‘Nonthing‘
而不是 ‘Value is‘ + (true ? ‘Something‘ : ‘Nonthing‘)
答案 ‘Something‘
第5題
1
|
var name = ‘World!‘;
|
這個相對簡單, 一個知識點:
- Hoisting
在 JavaScript中, functions 和 variables 會被提升。變量提升是JavaScript將聲明移至作用域 scope (全局域或者當前函數作用域) 頂部的行為。
這個題目相當於
1
|
var name = ‘World!‘;
|
所以答案是 ‘Goodbye Jack‘
第6題
1
|
var END = Math.pow(2, 53);
|
一個知識點:
- Infinity
在 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];
|
答案是 []
看一篇文章理解稀疏數組
- 譯 JavaScript中的稀疏數組與密集數組
- Array/filter
我們來看一下 Array.prototype.filter 的 polyfill:
1
|
if (!Array.prototype.filter) {
|
我們看到在叠代這個數組的時候, 首先檢查了這個索引值是不是數組的一個屬性, 那麽我們測試一下.
1
|
0 in ary; => true
|
也就是說 從 3 - 9 都是沒有初始化的’坑’!, 這些索引並不存在與數組中. 在 array 的函數調用的時候是會跳過這些’坑’的.
第8題
1
|
var two = 0.2
|
- JavaScript的設計缺陷?浮點運算:0.1 + 0.2 != 0.3
IEEE 754標準中的浮點數並不能精確地表達小數
那什麽時候精準, 什麽時候不經準呢? 筆者也不知道…
答案 [true, false]
第9題
1
|
function showCase(value) {
|
兩個知識點:
- Statements/switch
- String
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) {
|
一個知識點
- Arithmetic_Operators#Remainder
此題等價於
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
一個鮮為人知的實事: Array.prototype => []
;
—> 對JS原型的一些思考 by renaesop
答案: 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
|
兩個知識點:
- Arithmetic_Operators#Addition
- Arithmetic_Operators#Subtraction
+
用來表示兩個數的和或者字符串拼接, -
表示兩數之差.
請看例子, 體會區別:
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) {
|
第19題
1
|
function sidEffecting(ary) {
|
這是一個大坑, 尤其是涉及到 ES6語法的時候
知識點:
- Functions/arguments
首先 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
|
|
答案還是 111111111111111110000
. 解釋是 Lack of precision for numbers in JavaScript affects both small and big numbers.
但是筆者不是很明白……………. 請讀者賜教!
第21題
1
|
var x = [].reverse;
|
這個題有意思!
知識點:
- 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
.
補充:
@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
|
|
答案是 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;
|
知識點:
- Object/getPrototypeOf
只有 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
|
|
答案 false
第31題
1
|
function foo() { }
|
答案 [‘foo‘, ‘foo‘]
知識點:
- Function/name
因為函數的名字不可變.
第32題
1
|
"1 2 3".replace(/\d/g, parseInt)
|
知識點:
- String/replace#Specifying_a_function_as_a_parameter
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]+$/;
|
知識點:
- RegExp/test
這裏 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 的構造函數傳入一個字符串的話需要符合規範, 即滿足 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
- Math/max
有趣的是, Math.min 不傳參數返回 Infinity
, Math.max 不傳參數返回 -Infinity
??
答案: false
第41題
1
|
function captureOne(re, str) {
|
知識點:
- RegExp/exec
通俗的講
因為第一個正則有一個 g 選項 它會‘記憶’他所匹配的內容, 等匹配後他會從上次匹配的索引繼續, 而第二個正則不會
舉個例子
1
|
var myRe = /ab*/g;
|
所以 a1 = ‘1’; a2 = ‘1’; a3 = null; a4 = ‘2’
答案 [true, false]
第42題
1
|
var a = new Date("2014-03-19"),
|
這個….
JavaScript inherits 40 years old design from C: days are 1-indexed in C’s struct tm, but months are 0 indexed. In addition to that, getDay returns the 0-indexed day of the week, to get the 1-indexed day of the month you have to use getDate, which doesn’t return a Date object.
1
|
a.getDay()
|
都是套路!
答案 [false, false]
第43題
1
|
if (‘http://giftwrapped.com/picture.jpg‘.match(‘.gif‘)) {
|
知識點:
- String/match
String.prototype.match 接受一個正則, 如果不是, 按照 new RegExp(obj)
轉化. 所以 .
並不會轉義
那麽 /gif
就匹配了 /.gif/
答案: ‘a gif file‘
第44題
1
|
function foo(a) {
|
在兩個函數裏, a作為參數其實已經聲明了, 所以 var a; var a = ‘bye‘
其實就是 a; a =‘bye‘
所以答案 ‘hello‘, ‘bye‘
全部結束!
我的博客
44個javascript 變態題解析