JS simulation implements the new array method of ECMAScript5
- 2021-08-05 08:31:48
- OfStack
ECMAScript5 adds 10 new array methods that can only be used in ie9 and above browsers. Here is a mock implementation of these methods.
1. Array.isArray(element)
This method is used to determine whether the object passed in is of array type and returns true and false.
Array.newIsArray = function(element){
return Object.prototype.toString.call(element).slice(8,-1).toLocaleLowerCase() === 'array';
}
2. .indexOf(element)
This method is used to find the position of the passed-in object in the array and return that position. If it is not found, it returns-1. This method cannot be used to find undefined.
The indexOf method can be used with the ~ symbol. The bitwise operator ~ inverts the incoming number and subtracts it by 1, so-1 becomes 0, which is implicitly converted to false by putting it in the judgment condition.
Array.prototype.newIndexOf = function(element){
var index = -1;
for(var i = 0; i < this.length; i++){
if(this[i] === element && this[i] !== undefined){
index = i;
break;
}
}
return index;
};
var a = [1,2,3,4,,,5];
console.log(a.newIndexOf(undefined));
3. lastIndexOf(element)
This method has the same effect and return value as indexOf (element), except that it looks from right to left.
Array.prototype.newLastIndexOf = function(element){
var index = -1;
for(var i = this.length - 1; i >= 0; i--){
if(this[i] === element && this[i] !== undefined){
index = i;
break;
}
}
return index;
};
var a = [1,2,3,4,5,2,,,3];
console.log(a.newLastIndexOf(undefined));
4. forEach(function(element, index, array){})
Traversing the array with 1 callback function and 3 arguments: current element, current element index and the whole array. This method skips keeping missing members and will not destroy the original array.
Array.prototype.newForEach = function(fn){
for(var i = 0; i < this.length; i++){
if(i in this){
fn(this[i], i, this);
}
}
};
var a = [1,2,3,undefined,undefined,4,5,2,3];
a.forEach(function(e, i, arr){
console.log(e, i, arr);
})
5. every(function(element, index, array){})
The array is traversed using the callback function passed in, and the every method returns true when all callbacks return true, otherwise false. This method skips preserving missing members without destroying the original array.
Array.prototype.newEvery = function(fn){
var status = true;
for(var i = 0; i < this.length; i++){
if(i in this){
if(!(status = !!fn(this[i], i, this))){
break;
}
}
}
return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newEvery(function(){
console.log(arguments);
return 1;
}));
6. some (function (element, index, array) {})
The array is traversed using the callback function passed in, and the some method returns true when a callback returns true, otherwise false. This method skips preserving missing members without destroying the original array.
Array.prototype.newSome = function(fn){
var status = false;
for(var i = 0; i < this.length; i++){
if(i in this){
if(status = !!fn(this[i], i, this)){
break;
}
}
}
return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newSome(function(){
console.log(arguments);
return 0;
}));
7. map (function (element, index, array) {})
Use the callback function passed in to traverse the array and use the contents returned by traversing the array to form a new array. This method skips empty elements, but the final result preserves the position of missing members and does not destroy the original array.
Array.prototype.newMap = function(fn){
var array = new Array(this.length);
for(var i = 0; i < this.length; i++){
if(i in this){
array[i] = fn(this[i], i, this);
}
}
return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newMap(function(element, index, array){
console.log(arguments);
return element;
}))
8. filter(function(element, index, array){})
The callback function passed in is used to traverse the array, eventually returning a new array that contains all the elements that the callback function returned to true without destroying the original array.
Array.prototype.newFilter = function(fn){
var array = [];
for(var i = 0; i < this.length; i++){
if((i in this) && fn(this[i], i, this)){
array.push(this[i]);
}
}
return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newFilter(function(element, index, array){
console.log(arguments);
return element;
}))
9. reduce(function(accumulator, currentValue, currentIndex, array){})
Use the callback function passed in to traverse the array, returning the return value of the last callback function call, skipping missing members without destroying the original array.
Array.prototype.newReduce = function(fn){
if(this.length === 0){
throw new TypeError('Reduce of empty array with no initial value');
}
var result;
for(var i = 0; i < this.length; i++){
if(i in this){
if(!result){
result = this[i];
}else{
result = fn(result, this[i], i, this);
}
}
}
return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newReduce(function(a,b){
console.log(arguments);
return a + b;
}))
10. reduceRight(function(accumulator, currentValue, currentIndex, array){})
This method works the same as reduce, except that it is traversed from right to left.
Array.prototype.newReduceRight = function(fn){
if(this.length === 0){
throw new TypeError('Reduce of empty array with no initial value');
}
var result;
for(var i = this.length - 1; i >= 0; i--){
if(i in this){
if(!result){
result = this[i];
}else{
result = fn(result, this[i], i, this);
}
}
}
return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newReduceRight(function(a,b){
console.log(arguments);
return a + b;
}))