JS模擬實現ECMAScript5新增的數組方法
ECMAScript5 新增了十個數組方法,這些方法只有在ie9及以上瀏覽器中可以被使用,下面是對于這些方法的模擬實現。
一、Array.isArray(element)
該方法用于判斷傳入的對象是否為數組類型,返回true和false。
Array.newIsArray = function(element){
return Object.prototype.toString.call(element).slice(8,-1).toLocaleLowerCase() === 'array';
}
二、.indexOf(element)
該方法用于查找傳入對象在數組中的位置,并返回該位置,若沒有找到則返回-1,該方法不能用于尋找undefined。
indexOf方法可以和~符配合使用。按位運算符~會將傳入數字取反并減一,所以-1就會變成0,這時候把它放在判斷條件中會被隱式轉換為false。
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));
三、lastIndexOf(element)
該方法與indexOf(element)作用和返回值相同,唯一不同的地方是它是從右向左尋找。
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));
四、forEach(function(element, index, 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);
})
五、every(function(element, index, array){})
使用傳入的回調函數遍歷數組,當所有回調都返回true時,every方法返回true,否則返回false。該方法會跳過保留缺失成員,不會破壞原數組。
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;
}));
六、some(function(element, index, array){})
使用傳入的回調函數遍歷數組,當有回調返回true時,some方法返回true,否則返回false。該方法會跳過保留缺失成員,不會破壞原數組。
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;
}));
七、map(function(element, index, 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;
}))
八、filter(function(element, index, array){})
使用傳入的回調函數遍歷數組,最終返回一個新數組,該數組中包含所有回調函數返回true的元素,不會破壞原數組。
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;
}))
九、reduce(function(accumulator, currentValue, currentIndex, array){})
使用傳入的回調函數遍歷數組,返回最后一個回調函數調用的返回值,跳過缺失成員,不會破壞原數組?!?br />
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;
}))
十、reduceRight(function(accumulator, currentValue, currentIndex, array){})
該方法作用于reduce相同,唯一區(qū)別是它是從右往左開始遍歷。
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;
}))
以上所述是小編給大家介紹的JS模擬實現ECMAScript5新增的數組方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
深入淺析JavaScript系列(13):This? Yes,this!
在這篇文章里,我們將討論跟執(zhí)行上下文直接相關的更多細節(jié)。討論的主題就是this關鍵字。實踐證明,這個主題很難,在不同執(zhí)行上下文中this的確定經常會發(fā)生問題2016-01-01

