13.2.2 prototype属性.html

来自「javascript即用即差核心对象手册」· HTML 代码 · 共 39 行

HTML
39
字号
<script>
  //添加一个属性,用于统计删除掉元素地个数
  Array.prototype.removed=0;
  //添加一个方法,用于删除指定索引的元素
  Array.prototype.removeAt=function(index)
  {
    if(isNaN(index)||index<0)
	    {return false;}
	if(index>=this.length)
	    {index=this.length-1}
    for(var i=index;i<this.length;i++)
    {
       this[i]=this[i+1];
    }
    this.length-=1
	this.removed++;
  }
  //添加一个方法,输出数组中的全部数据
  Array.prototype.outPut=function(sp)
  {
      for(var i=0;i<this.length;i++)
	  {
	     document.write(this[i]);
		 document.write(sp);
	  }
	  document.write("<br>");
  }
  //定义数组
   var arr=new Array(1,2,3,4,5,6,7,8,9);
   //测试添加的方法和属性
   arr.outPut(" ");
   document.write("删除一个数据<br>");
   arr.removeAt(2);
   arr.outPut(" ");
   arr.removeAt(4);
   document.write("删除一个数据<br>");
   arr.outPut(" ")
   document.write("一共删除了"+arr.removed+"个数据");
</script>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?