原文發佈於個人 githubjavascript
承接上篇 44個 Javascript 變態題解析 (上)java
[1 < 2 < 3, 3 < 2 < 1]
這個題也還能夠.git
這個題會讓人誤覺得是 2 > 1 && 2 < 3
其實不是的.github
這個題等價於編程
1 < 2 => true; true < 3 => 1 < 3 => true; 3 < 2 => false; false < 1 => 0 < 1 => true;
答案是 [true, true]
數組
// the most classic wtf 2 == [[[2]]]
這個題我是猜的. 我猜的 true
, 至於爲何.....瀏覽器
both objects get converted to strings and in both cases the resulting string is "2"
我不能信服...app
3.toString() 3..toString() 3...toString()
這個題也挺逗, 我作對了 :) 答案是 error, '3', error
函數
你若是換一個寫法就更費解了測試
var a = 3; a.toString()
這個答案就是 '3'
;
爲啥呢?
由於在 js 中 1.1
, 1.
, .1
都是合法的數字. 那麼在解析 3.toString
的時候這個 .
究竟是屬於這個數字仍是函數調用呢? 只能是數字, 由於3.
合法啊!
(function(){ var x = y = 1; })(); console.log(y); console.log(x);
答案是 1, error
y 被賦值到全局. x 是局部變量. 因此打印 x 的時候會報 ReferenceError
var a = /123/, b = /123/; a == b a === b
即便正則的字面量一致, 他們也不相等.
答案 false, false
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
var a = {}, b = Object.prototype; [a.prototype === b, Object.getPrototypeOf(a) === b]
知識點:
只有 Function 擁有一個 prototype 的屬性. 因此 a.prototype
爲 undefined
.
而 Object.getPrototypeOf(obj)
返回一個具體對象的原型(該對象的內部[[prototype]]
值)
答案 false, true
function f() {} var a = f.prototype, b = Object.getPrototypeOf(f); a === b
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 函數的原型.
請看:
a === Object.getPrototypeOf(new f()) // true b === Function.prototype // true
答案 false
function foo() { } var oldName = foo.name; foo.name = "bar"; [oldName, foo.name]
答案 ['foo', 'foo']
知識點:
由於函數的名字不可變.
"1 2 3".replace(/\d/g, parseInt)
知識點:
str.replace(regexp|substr, newSubStr|function)
若是replace函數傳入的第二個參數是函數, 那麼這個函數將接受以下參數
match 首先是匹配的字符串
p1, p2 .... 而後是正則的分組
offset match 匹配的index
string 整個字符串
因爲題目中的正則沒有分組, 因此等價於問
parseInt('1', 0) parseInt('2', 2) parseInt('3', 4)
答案: 1, NaN, 3
function f() {} var parent = Object.getPrototypeOf(f); f.name // ? parent.name // ? typeof eval(f.name) // ? typeof eval(parent.name) // ?
先說如下答案 'f', 'Empty', 'function', error
這個答案並不重要.....
這裏第一小問和第三小問很簡單不解釋了.
第二小問筆者在本身的瀏覽器測試的時候是 ''
, 第四問是 'undefined'
因此應該是平臺相關的. 這裏明白 parent === Function.prototype
就行了.
var lowerCaseOnly = /^[a-z]+$/; [lowerCaseOnly.test(null), lowerCaseOnly.test()]
知識點:
這裏 test 函數會將參數轉爲字符串. 'nul'
, 'undefined'
天然都是全小寫了
答案: true, true
[,,,].join(", ")
[,,,] => [undefined × 3]
由於javascript 在定義數組的時候容許最後一個元素後跟一個,
, 因此這是個長度爲三的稀疏數組(這是長度爲三, 並無 0, 1, 2三個屬性哦)
答案: ", , "
var a = {class: "Animal", name: 'Fido'}; a.class
這個題比較流氓.. 由於是瀏覽器相關, class
是個保留字(如今是個關鍵字了)
因此答案不重要, 重要的是本身在取屬性名稱的時候儘可能避免保留字. 若是使用的話請加引號 a['class']
var a = new Date("epoch")
知識點:
簡單來講, 若是調用 Date 的構造函數傳入一個字符串的話須要符合規範, 即知足 Date.parse 的條件.
另外須要注意的是 若是格式錯誤 構造函數返回的還是一個Date 的實例 Invalid Date
.
答案 Invalid Date
var a = Function.length, b = new Function().length a === b
咱們知道一個function(Function 的實例)的 length
屬性就是函數簽名的參數個數, 因此 b.length == 0.
另外 Function.length 定義爲1......
因此不相等.......答案 false
var a = Date(0); var b = new Date(0); var c = new Date(); [a === b, b === c, a === c]
仍是關於Date 的題, 須要注意的是
若是不傳參數等價於當前時間.
若是是函數調用 返回一個字符串.
答案 false, false, false
var min = Math.min(), max = Math.max() min < max
知識點:
有趣的是, Math.min 不傳參數返回 Infinity
, Math.max 不傳參數返回 -Infinity
?
答案: false
function captureOne(re, str) { var match = re.exec(str); return match && match[1]; } var numRe = /num=(\d+)/ig, wordRe = /word=(\w+)/i, a1 = captureOne(numRe, "num=1"), a2 = captureOne(wordRe, "word=1"), a3 = captureOne(numRe, "NUM=2"), a4 = captureOne(wordRe, "WORD=2"); [a1 === a2, a3 === a4]
知識點:
通俗的講
由於第一個正則有一個 g 選項 它會‘記憶’他所匹配的內容, 等匹配後他會從上次匹配的索引繼續, 而第二個正則不會
舉個例子
var myRe = /ab*/g; var str = 'abbcdefabh'; var myArray; while ((myArray = myRe.exec(str)) !== null) { var msg = 'Found ' + myArray[0] + '. '; msg += 'Next match starts at ' + myRe.lastIndex; console.log(msg); } // Found abb. Next match starts at 3 // Found ab. Next match starts at 9
因此 a1 = '1'; a2 = '1'; a3 = null; a4 = '2'
答案 [true, false]
var a = new Date("2014-03-19"), b = new Date(2014, 03, 19); [a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]
這個....
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.
a.getDay() 3 b.getDay() 6 a.getMonth() 2 b.getMonth() 3
都是套路!
if ('http://giftwrapped.com/picture.jpg'.match('.gif')) { 'a gif file' } else { 'not a gif file' }
知識點:
String.prototype.match 接受一個正則, 若是不是, 按照 new RegExp(obj)
轉化. 因此 .
並不會轉義
那麼 /gif
就匹配了 /.gif/
答案: 'a gif file'
function foo(a) { var a; return a; } function bar(a) { var a = 'bye'; return a; } [foo('hello'), bar('hello')]
在兩個函數裏, a做爲參數其實已經聲明瞭, 因此 var a; var a = 'bye'
其實就是 a; a ='bye'
因此答案 'hello', 'bye'
所有結束!
==================
因爲筆者水平有限, 若是解釋有誤, 還望指出 ?
經過整理, 筆者發現絕大部分題目都是由於本身對於基礎知識或者說某個 API 的參數理解誤差才作錯的.
筆者的重災區在原型那一塊, 因此此次被虐和整理仍是頗有意義呀.
筆者相信 堅實的基礎是深刻編程的前提. 因此基礎書仍是要常看啊 ?
最後這些變態題如今看看還變態嘛?