js为Array数组添加remove函数
js默认是没有remove函数的,会出现
Uncaught TypeError: arr.remove is not a function
解决方法大家应该都会,但是为了更方便,可以添加prototype,具体如下:
Array.prototype.remove = function () {
for (var i = 0; i < arguments.length; i++) {
var ele = arguments[i];
var index = this.indexOf(ele);
if (index > -1) {
this.splice(index, 1);
}
}
};
var arr = [1,2,3,4,5];
arr.remove(1);
alert(arr);