Es6源碼--問題記錄--關於includes、indexOf

問題一:
這是一個烏龍問題。

function(){
    console.log([].includes(NaN));  //true 實驗結果
    console.log([].includes(3));    //false 實驗結果
}(1,2,4,5,NaN)

相當納悶,參數是怎麼傳出去的。。。是否存在我還不知道的潛規則??
as we know
function 有一個屬性——arguments:包含所有傳進方法的參數
一開始我想源碼裏是不是這樣寫了

Array.prototype.includes = function(){
    //value:調用此方法對象
    var value = this || !function(){
        var tempArr = []
        for(var i =0;i<arguments.length,i++){
            tempArr.push(arguments[i]);
        }
        return tempArr
    }()
    ...
}

本着格物致知的原則,確定一下,沒找到源碼。

回顧看了一下

https://developer.mozilla.org/


示例代碼

(function() {
  console.log([].includes.call(arguments, 'a')); // true
  console.log([].includes.call(arguments, 'd')); // false
})('a','b','c');

原來用了call

那麼問題又來了
includes()源碼到底怎麼寫的。
僞代碼如下

Let O be ? ToObject(this value).
Let len be ? ToLength(? Get(O, "length")).
If len is 0, return false.
Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step produces the value 0.)
If n ≥ 0, then 
    Let k be n.
Else n < 0,
    Let k be len + n.
    If k < 0, let k be 0.
Repeat, while k < len
    Let elementK be the result of ? Get(O, ! ToString(k)).
    If SameValueZero(searchElement, elementK) is true, return true.
    Increase k by 1.
Return false.

查詢一下SameValueZero
是Es6的方法

SameValueZero: used by %TypedArray% and ArrayBuffer constructors, as well as Map and Set operations, and also String.prototype.includes and Array.prototype.includes since ES2016

what’s it?

http://www.ecma-international.org/ecma-262/7.0/


The internal comparison abstract operation SameValueZero(x, y), where x and y are ECMAScript language values, produces true or false. Such a comparison is performed as follows:

If Type(x) is different from Type(y), return false.
If Type(x) is Number, then
    If x is NaN and y is NaN, return true.
    If x is +0 and y is -0, return true.
    If x is -0 and y is +0, return true.
    If x is the same Number value as y, return true.
    Return false.
Return SameValueNonNumber(x, y).

NOTE [筆記]
SameValueZero differs from SameValue only in its treatment of +0 and -0.

歐克
這就可以解決了——爲什麼includes() 可以判斷NaN是否在數組裏

REMAKE [備註]

→7.2.11SameValueNonNumber (x, y)

The internal comparison abstract operation SameValueNonNumber(x, y), where neither x nor y are Number values, produces true or false. Such a comparison is performed as follows:

Assert: Type(x) is not Number.
Assert: Type(x) is the same as Type(y).
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is String, then
    If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
If Type(x) is Boolean, then

    If x and y are both true or both false, return true; otherwise, return false.
If Type(x) is Symbol, then

    If x and y are both the same Symbol value, return true; otherwise, return false.

Return true if x and y are the same Object value. Otherwise, return false.

人不裝B僞少年,by h.k

2017年12月29日
看源碼是愛好、解決問題是職業——h.k

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章