JavaScript 5 新增 Array 方法實現(xiàn)介紹
更新時間:2012年02月06日 11:42:29 作者:
JavaScript 5 發(fā)布有一段時間了,Array 對象新增了很多方法。但在老版本的瀏覽器上還不能使用,得益于 JavaScript 的動態(tài)可擴(kuò)展性,我實現(xiàn)了這些方法,在此曬出來與君共勉
復(fù)制代碼 代碼如下:
/*!
* jLip JavaScript Library v0.1
*
* Copyright 2012, Lip2up (lip2up@qq.com)
* Just for free use, NO LICENSE
*/
(function() {
function extend(target, props) {
for (var m in props) {
if (target[m] === undefined) target[m] = props[m];
}
}
var fns = { every: 1, some: 2, forEach: 3, map: 4, filter: 5 },
reduceError = 'Reduce of empty array with no initial value';
function each(fn, _this, kind) {
var len = this.length, ret = kind == fns.filter ? []
: kind == fns.map ? Array(len) : undefined,
find = kind == fns.some, i, v;
for (i = 0; i < len; i++) {
if (this[i] !== undefined) {
v = fn.call(_this, this[i], i, this);
switch (kind) {
case fns.every:
case fns.some:
if (v === find) return find;
break;
case fns.map:
ret[i] = v;
break;
case fns.filter:
if (v === true) ret[ret.length] = this[i];
break;
}
}
}
return kind >= fns.forEach ? ret : !find;
}
function reduce(fn, init, right) {
var len = this.length, i, prev, inc = right ? -1 : 1;
if (len == 0 && init === undefined)
throw TypeError(reduceError);
for (i = right ? len - 1 : 0, prev = init;
prev === undefined && (right ? i >= 0 : i < len);
i += inc) {
prev = this[i];
}
if (prev === undefined && i == (right ? -1 : len))
throw TypeError(reduceError);
for (; (right ? i >= 0 : i < len); i += inc) {
if (this[i] !== undefined)
prev = fn(prev, this[i], i, this);
}
return prev;
}
extend(Array.prototype, {
every: function(fn, _this) {
return each.call(this, fn, _this, fns.every);
},
some: function(fn, _this) {
return each.call(this, fn, _this, fns.some);
},
forEach: function(fn, _this) {
return each.call(this, fn, _this, fns.forEach);
},
map: function(fn, _this) {
return each.call(this, fn, _this, fns.map);
},
filter: function(fn, _this) {
return each.call(this, fn, _this, fns.filter);
},
reduce: function(fn, init) {
return reduce.call(this, fn, init);
},
reduceRight: function(fn, init) {
return reduce.call(this, fn, init, true);
}
});
})();
您可能感興趣的文章:
- javascript Array.prototype.slice的使用示例
- Javascript中判斷變量是數(shù)組還是對象(array還是object)
- JavaScript字符串String和Array操作的有趣方法
- javascript object array方法使用詳解
- javascript學(xué)習(xí)筆記(五) Array 數(shù)組類型介紹
- JavaScript高級程序設(shè)計 讀書筆記之九 本地對象Array
- JavaScript Array Flatten 與遞歸使用介紹
- JavaScript中的Array對象使用說明
- javaScript array(數(shù)組)使用字符串作為數(shù)組下標(biāo)的方法
相關(guān)文章
關(guān)于javascript的一些知識以及循環(huán)詳解
下面小編就為大家?guī)硪黄P(guān)于javascript的一些知識以及循環(huán)詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
JavaScript中arguments和this對象用法分析
這篇文章主要介紹了JavaScript中arguments和this對象用法,結(jié)合實例形式較為詳細(xì)的分析了arguments對象和this對象的功能、常見用法及相關(guān)操作注意事項,需要的朋友可以參考下2018-08-08
D3.js實現(xiàn)餅圖,環(huán)圖,玫瑰圖的繪制
這篇文章主要為大家介紹了如何利用D3.js中的d3.pie和d3.arc實現(xiàn)餅圖、環(huán)圖和玫瑰圖的繪制,文中的實現(xiàn)方法講解詳細(xì),感興趣的可以嘗試一下2022-11-11
給easyui的datebox控件添加清空按鈕的實現(xiàn)方法
下面小編就為大家?guī)硪黄oeasyui的datebox控件添加清空按鈕的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

