js强加方法

 1 (function () {
 2 
 3     Array.prototype.removeItem=function (item) {
 4         var index=this.indexOf(item);
 5         if(index!=-1){
 6             this.splice(index,1);
 7         }
 8     };
 9 
10     var arr=["a","b","c","d"];
11     arr.removeItem("b");
12     console.log(arr);
13 })();

一句话解释prototype,为一个对象强加方法。

代码中强行为系统的Array对象添加了removeItem方法。

以后使用数组的时候,导入此js文件,便可以对数组进行移除数据项的操作了。╮(╯▽╰)╭

原文地址:https://www.cnblogs.com/chenluomenggongzi/p/5811509.html