Array.prototype.remove=function(element){}

来源:互联网 发布:淘宝网平板电脑版下载 编辑:程序博客网 时间:2024/06/08 15:31
<!DOCTYPE html><html><head>    <meta charset="utf-8"></head><title>prototype和splice</title><body>  <!--  代码放在body中,可运行 -->   <script type="text/javascript">        /*Array.prototype.remove=function(element){}扩展了javascript的内置对象Array,这样,我们以后声明的所有数组对象都会自动地拥有自定义的remove(element)方法*/        Array.prototype.remove=function(element){             for(i=0;i<this.length;i++){//遍历数组对象的元素                if(this[i]==element){  //当相等时                   this.splice(i,1);   //调用javascript的内置方法splice(规定要删除的项目的位置,要删除的项目数量),返回删除后的this。                }             }             return this;   //返回调用remove方法的对象        }   </script></body>
阅读全文
0 0