Welcome to Alex's Midway

手写Array.prototype.some

May 24, 2020


前端知识复习 code snippet

array.prototype.some 标准

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. Let k be 0.
  5. Repeat, while k < len,

    • Let Pk be ! ToString(k).
    • Let kPresent be ? HasProperty(O, Pk).
    • If kPresent is true, then

      • Let kValue be ? Get(O, Pk).
      • Let testResult be ! ToBoolean(? Call(callbackfn, thisArg, « kValue, k, O »)).
      • If testResult is true, return true.
  6. Set k to k + 1.
  7. Return false.

代码

Array.prototype.mySome = function(callbackfn, thisArg){
    if(this == undefined){
        throw TypeError('Cannot read property filter of '+ this);
    }
    if(typeof callbackfn !== 'function'){
        throw TypeError(callbackfn + ' is not a function')
    }

    var O = Object(this);
    var len = O.length >>> 0;
    for(var k=0;k<len;k++){
        if(k in O){
            var kValue = O[k];
            if(callbackfn.call(thisArg, kValue,k,O)) return true;
        }
    }
    return false;
}