Javascript common functions

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.       

1. Array.prototype.removeAt = function (index) {
   var part1 = this.slice(0, index);
   var part2 = this.slice(index + 1);
   return part1.concat(part2);

}

var a = [10, 11,12,13];
a.removeAt(2); // remove 12

 

2. Array.prototype.indexOf = function (what, i) {
    i = i || 0;
    var L = this.length;
    while (i < L) {
        if (this[i] === what) return i;
        ++i;
    }
    return -1;
}

3. Array.prototype.remove = function () {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) != -1) {
            this.splice(ax, 1);
        }
    }
    return this;
}

你可能感兴趣的:(JavaScript)