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 就是得到这个参数序列组
{
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 就是得到这个参数序列组
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);
}
请参阅
0…n 属性 | callee 属性 | length 属性
要求
版本 1
该对象代表正在执行的函数和调用它的函数的参数。[function.]arguments[n]
参数
function
可选项。当前正在执行的 Function 对象的名字。
n
必选项。要传递给 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); // 返回参数列表。
}
因为alert()命令在循环体外了,j的值已经变成undefined
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);没有执行不知道什么原因呢
{
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]有什么意思呢 怎么返回的值又成了参数的个数!真是很奇怪阿!
这个函数不就是返回实际的参数的个数吗,他有什么实际的意义呢!
{
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]有什么意思呢 怎么返回的值又成了参数的个数!真是很奇怪阿!
这个函数不就是返回实际的参数的个数吗,他有什么实际的意义呢!
this 代表了instance这个数组本身,this.length代表了数组的长度,可是,他是多少呢,是一开始的5呢,还是随着i的变化变呢,如果是随着i的变化变,那么这不是重复劳动吗,人家原来就是这个样子阿,如果是5,那么,岂不是把instance的值全都试验了一次this[4]=argument[4]阿他为什么不开始就返回argument.length呢