手写Array.prototype.map
May 20, 2020
前端知识复习 code snippet
废话少说,先看 array.prototype.map 标准
- Let O be ? ToObject(this value).
- Let len be ? LengthOfArrayLike(O).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- Let A be ? ArraySpeciesCreate(O, len).
- 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).
- Let mappedValue be ? Call(callbackfn, thisArg, « kValue, k, O »).
- Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
- Set k to k +1.
- Return A.
代码
Array.prototype.myMap = function(callbackfn, thisArg) {
// this cannot be null or undefined
if (this == null) {
throw new TypeError('this is null or undefined');
}
var O = Object(this);
// convert O.length to integer 0
// e.g. undefined >>> 0 => 0
var len = O.length >>> 0;
// callbackfn must be a function
if (typeof callbackfn != 'function') {
throw new TypeError(callbackfn + ' is not a function');
}
var T = thisArg != undefined ? thisArg : undefined;
var A = new Array(len);
for (var k = 0; k < len; k++) {
// iterate all property of O, including props in prototype chain
if (k in O) {
var kValue = O[k];
var mappedValue = callbackfn.call(T, kValue, k, O);
A[k] = mappedValue;
}
}
return A;
};