JavaScript的prototype实现移除数组元素

创建时间:2015/7/12 16:29:13
关键字:JavaScript,prototype,原型

/*对原数组操作,移除元素*/
Array.prototype.remove = function (item) {
    var n = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i] != item) {
            this[n++] = this[i];
        }
    }
    this.length -= 1;
}  

/*移除指定元素,返回新的数组*/
Array.prototype.remove = function (obj) {
    var a = [];
    for (var i = 0; i < this.length; i++) {
        if (this[i] != obj) {
            a.push(this[i]);
        }
    }
    return a;
}