The map method
A new array consisting of the result of each element of the original array executing the callback function
ThisArg (optional) : the value is used as this when the callback function is executed
Array.prototype.map = function (callbackFn, thisArg) {
// Handle an array type exception
if (this= = =null || this= = =undefined) {
throw new TypeError("Cannot read property 'map' of null or undefined")}// Handle callback type exceptions
if (Object.prototype.toString.call(callbackFn) ! ="[object Function]") {
throw new TypeError(callbackFn + "is not a function");
}
// Convert to object first
let O = Object(this);
let T = thisArg;
let len = O.length >>> 0;
let A = new Array(len);
for (let k = 0; k < len; k++) {
HasOwnProperty: indicates that only private properties can be found
for (k in O) {
let kValue = O[k];
letmappedValue = callbackFn.call(T, kValue, k, O); A[k] = mappedValue; }}return A;
}
Copy the code
The reduce method
Execute a callback function provided by you on each element in the array, summarizing the result into a single return value
//initalValue: The value of the first argument when the callback function is first called. If no initial value is provided, the first element in the array is used.
Array.prototype.Myreduce = function (callbackfn, initialValue) {
if (this= = =null || this= = =undefined) {
throw new TypeError("Cannot read property 'reduce' of null or undefined");
}
if (Object.prototype.toString.call(callbackfn) ! ='[object Function]') {
throw new TypeError(callbackfn + ' is not a function')}let O = Object(this);
let len = O.length >>> 0;
let k = 0;
let accumulator = initialValue;
// initialValue is null, the first element in the array is used.
if (accumulator === undefined) {
for (; k < len; k++) {
// Find the prototype chain
if (k in O) {
accumulator = O[k];
k++;
break; }}}Calling reduce on an empty array with no initial value will result in an error
if (k == len && accumulator === undefined) {
throw new TypeError('Each element of the array is empty');
}
for (; k < len; k++) {
if (k in O) {
// This is an accumulator
accumulator = callbackfn.call(undefined, accumulator, O[k], k, O); }}return accumulator;
}
Copy the code
The filter method
Create a new array that contains all the elements of the test implemented by providing the function
ThisArg: the value used for this when callback is executed
Array.prototype.Myfilter = function (callbackfn, thisArg) {
// Handle an array type exception
if (this= = =null || this= = =undefined) {
throw new TypeError("Cannot read property 'filter' of null or undefined");
}
// Handle callback type exceptions
if (Object.prototype.toString.call(callbackfn) ! ='[object Function]') {
throw new TypeError(callbackfn + "is not a function");
}
let O = Object(this);
let len = O.length >>> 0;
let resLen = 0;
let res = [];
for (let i = 0; i < len; i++) {
if (i in O) {
let elment = O[i];
if(callbackfn.call(thisArg, O[i], i, O)) { res[resLen++] = elment; }}}return res;
}
Copy the code
Push method
Returns the length of the new array
Array.prototype.mypush = function (. items) {
let O = Object(this);
let len = O.length >>> 0;
// The length of the argument
let argCount = items.length >>> 0;
if (len + argCount > 2 ^ 52 - 1) {
throw new TypeError("The number of array is over the max value restricted!");
}
for (let i = 0; i < argCount; i++) {
O[len + i] = items[i];
}
let newLength = len + argCount;
O.length = newLength;
return newLength;
}
Copy the code
The pop method
Returns the deleted element
Array.prototype.pop = function () {
let O = Object(this);
let len = O.length >>> 0;
if (len === 0) {
O.length = 0;
return undefined;
}
len--;
// The last value of the array
let value = O[len];
delete O[len];
O.length = len;
return value;
}
Copy the code