<script language="javaScript">
function aa(){
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from; //return this.push(rest);//为什么不调用这个方法,跟下面的方法有区别么?
return this.push.apply(this, rest);
};
var array = ["one", "two", "three", "four", "five", "six"]; array.remove(0);//删除第一个元素
document.write(array+"<br/>");
array.remove(-1);//删除倒数第一个元素
document.write(array+"<br/>");
array.remove(0,2);//删除数组中下标为0-2的元素(3个)
document.write(array);
}
</script>
<BODY>
<form id="form1" name="form1" method="post" action="">
<input type="submit" name="Submit" value="删除" onclick="return aa();"/>
</form>
</BODY>
这个js删除函数中的
return this.push.apply(this, rest); 这一句能写成this.push(rest)么,为什么那样的话结果不对啦?
function aa(){
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from; //return this.push(rest);//为什么不调用这个方法,跟下面的方法有区别么?
return this.push.apply(this, rest);
};
var array = ["one", "two", "three", "four", "five", "six"]; array.remove(0);//删除第一个元素
document.write(array+"<br/>");
array.remove(-1);//删除倒数第一个元素
document.write(array+"<br/>");
array.remove(0,2);//删除数组中下标为0-2的元素(3个)
document.write(array);
}
</script>
<BODY>
<form id="form1" name="form1" method="post" action="">
<input type="submit" name="Submit" value="删除" onclick="return aa();"/>
</form>
</BODY>
这个js删除函数中的
return this.push.apply(this, rest); 这一句能写成this.push(rest)么,为什么那样的话结果不对啦?
var arr = [];
arr.push([1, 2, 3]);
和
arr.push(1, 2, 3);
的区别。apply就是将一个数组参数,变成多个参数传递给函数。
arr.push([1, 2, 3]); -> arr.push(1, 2, 3);
call是接收每一个参数
当不传递参数时用哪一个都一样。
把rest数组当成一个元素推入this----------
return this.push.apply(this, rest);
把数组里的每个元素推入this