js Array扩展方法
Object.extend = function (destination, source) {
?/// <summary>
??? /// 擴(kuò)展對(duì)象方法
??? /// </summary>
??? for (var property in source) {
??????? destination[property] = source[property];
??? }
??? return destination;
}
Object.extend(Array.prototype, {
?each: function (iterator) {
??/// <summary>
??/// 遍歷數(shù)組執(zhí)行方法
??/// </summary>
??for (var i = 0, length = this.length; i < length; i++)
????? iterator(this[i]);
?},
?clear: function () {
??/// <summary>
??/// 清空數(shù)組
??/// </summary>
??this.length = 0;
??return this;
?},
?indexOf: function (object) {
??/// <summary>
??/// 獲取某項(xiàng)在數(shù)組中的位置
??/// </summary>
??for (var i = 0, length = this.length; i < length; i++)
????? if (this[i] == object) return i;
??return -1;
?},
?remove: function (dx) {
??/// <summary>
??/// 移除指定索引出的對(duì)象
??/// </summary>
??if (isNaN(dx) || dx < 0 || dx > this.length) { return false; }
??for (var i = 0, n = 0; i < this.length; i++) {
????? if (this[i] != this[dx]) {
????????? this[n++] = this[i]
????? }
??}
??this.length -= 1
?},
?insertAt: function(index,obj){
??/// <summary>
??/// 在index處插入元素
??/// </summary>??
??this.splice(index,0,obj);???
?},
?removeAt: function(index){
??/// <summary>
??/// 移除index處元素
??/// </summary>???
??this.splice(index,1);???
?}
})
總結(jié)
以上是生活随笔為你收集整理的js Array扩展方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Could NOT find SDL_i
- 下一篇: NAGA-Ⅱ与QPSO算法求解下层为非合