手写Array.prototype.forEach
May 23, 2020
前端知识复习 code snippet
- Let O be ? ToObject(this value).
- Let len be ? LengthOfArrayLike(O).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- Let k be 0.
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).
- Perform ? Call(callbackfn, thisArg, « kValue, k, O »).
- Set k to k + 1.
- Return undefined.
代码
Array.prototype.myForEach = function (callbackfn, thisArg) {
// this must not be null or undefined
if (this == null) {
throw new TypeError(
'Array.prototype.forEach called on null or undefined'
);
}
// callbackfn must be a function
if (typeof callbackfn !== 'function') {
throw new TypeError(callbackfn + ' is not a function');
}
var O = Object(this);
var len = O.length >>> 0;
var k = 0;
// iterate array
if (thisArg === undefined) {
for (; k < len; k++) {
if (k in O) {
var kValue = O[k];
callbackfn(kValue, k, O);
}
}
} else {
for (; k < len; k++) {
if (k in O) {
var kValue = O[k];
callbackfn.call(thisArg, kValue, k, O);
}
}
}
return undefined;
};