if(typeof(Array.prototype.push)!="function")
{
  Array.prototype.push = function()
  {
    for (var i=0; i<arguments.length; i++)
      this[this.length] = arguments[i];
    return this.length; 
  };
}var instances=[0,1,9];
instances.push(9,8,7,6,5,4,3,2,1);  //你看, 这里传递的参数个数与你定义时的个数是不相同的, 用 arguments 就是得到这个参数序列组

解决方案 »

  1.   

    为当前执行的 function 对象返回一个arguments 对象。function.arguments
    function 参数是当前执行函数的名称,可以省略。说明
    通过 arguments 属性,函数可以处理可变数量的参数。arguments 对象的 length 属性包含了传递给函数的参数的数目。对于arguments 对象所包含的单个参数,其访问方法与数组中所包含的参数的访问方法相同。示例
    下面的例子说明了 arguments 属性的用法:function ArgTest(){
       var i, s, numargs = arguments.length;
       s = numargs;  
       if (numargs < 2)
          s += " argument was passed to ArgTest. It was ";
       else
          s += " arguments were passed to ArgTest. They were " ;
       for (i = 0; i < numargs; i++)
          {
             s += arguments[i] + " ";
          }
       return(s);
    }
      

  2.   

    arguments 对象
    请参阅
    0…n 属性 | callee 属性 | length 属性
    要求
    版本 1
    该对象代表正在执行的函数和调用它的函数的参数。[function.]arguments[n]
    参数
    function 
    可选项。当前正在执行的 Function 对象的名字。 

    必选项。要传递给 Function 对象的从0开始的参数值索引。 
    说明
    不能显式创建 arguments 对象。arguments 对象只有函数开始时才可用。函数的 arguments 对象并不是一个数组,访问单个参数的方式与访问数组元素的方式相同。索引 n 实际上是 arguments 对象的 0…n 属性的其中一个参数。示例
    下面的示例演示了 arguments 对象的用法。function ArgTest(a, b){
       var i, s = "The ArgTest function expected ";
       var numargs = arguments.length;     // 获取被传递参数的数值。
       var expargs = ArgTest.length;       // 获取期望参数的数值。
       if (expargs < 2)
          s += expargs + " argument. ";
       else
          s += expargs + " arguments. ";
       if (numargs < 2)
          s += numargs + " was passed.";
       else
          s += numargs + " were passed.";
       s += "\n\n"
       for (i =0 ; i < numargs; i++){      // 获取参数内容。
       s += "  Arg " + i + " = " + arguments[i] + "\n";
       }
       return(s);                          // 返回参数列表。
    }
      

  3.   

    为什么我alert(j);提示我没有定义呢
    因为alert()命令在循环体外了,j的值已经变成undefined
      

  4.   

    to  meizz(梅花雪) 您好,我测试了 果然不同,结果是12,也就是把我原先定义的参数个数也计算在里面了阿
      

  5.   

    <script language=javascript>
    if(typeof(Array.prototype.push)!="function")
    {
      Array.prototype.push = function()
      { alert('ddd');
        for (var i=0; i<arguments.length; i++)
         {this[this.length] = arguments[i];
          } 
        
    return this.length; 
      };
    }
     var instances=[8,0,7,67];
     xx=instances.push(9,8,7,6,5,4,3,2,1);
    alert(xx);
     </script>你看我这样写的,返回的是12 而且,我在函数开始的时候alert(ddd);没有执行不知道什么原因呢
      

  6.   

    Array.prototype.push 这个方法在高版本的IE里是一个系统方法, 所以就没有调用你自己定义的 push 了. 你可以把外面套着的那个 if 判断去掉再试试
      

  7.   

    Array.prototype.push = function()
      {
        for (var i=0; i<arguments.length; i++)
          this[this.length] = arguments[i];
        return this.length; 
      };
    按照我的理解,我解释一下:
    比如instance=[3,5,6,7,6]
    当instance.push的时候, arguments.length=5也就是实际传递的参数个数,
    下面的一句我就不明白了,  this[this.length] = arguments[i]; //arguments[i]不是里面的值吗,怎么会赋予this[this.length]呢,比如,如果i=3的时候 arguments[3]不是等于7吗,那把他赋予this[this.length]有什么意思呢 怎么返回的值又成了参数的个数!真是很奇怪阿!
    这个函数不就是返回实际的参数的个数吗,他有什么实际的意义呢! 
      

  8.   

    Array.prototype.push = function()
      {
        for (var i=0; i<arguments.length; i++)
          this[this.length] = arguments[i];
        return this.length; 
      };
    按照我的理解,我解释一下:
    比如instance=[3,5,6,7,6]
    当instance.push的时候, arguments.length=5也就是实际传递的参数个数,
    下面的一句我就不明白了,  this[this.length] = arguments[i]; //arguments[i]不是里面的值吗,怎么会赋予this[this.length]呢,比如,如果i=3的时候 arguments[3]不是等于7吗,那把他赋予this[this.length]有什么意思呢 怎么返回的值又成了参数的个数!真是很奇怪阿!
    这个函数不就是返回实际的参数的个数吗,他有什么实际的意义呢! 
      

  9.   

    Array.prototype.push 这是给 Array 对象扩展一个 push 方法, 这是一种面向对象式的写法, 里面的 this 就是 Array 本身, 比如下面的 instance.push() 调用时, 这个 this 就是指数组 instance, 至于为什么要返回数组的长度, 这个是保持与系统里的那个数组push()一致, 系统里的 push就是返回改变后的数组长度的.
      

  10.   

    恩,我不太明白的就是this[this.length]这数组 instance=[3,5,6,7,6]
    this 代表了instance这个数组本身,this.length代表了数组的长度,可是,他是多少呢,是一开始的5呢,还是随着i的变化变呢,如果是随着i的变化变,那么这不是重复劳动吗,人家原来就是这个样子阿,如果是5,那么,岂不是把instance的值全都试验了一次this[4]=argument[4]阿他为什么不开始就返回argument.length呢