(1) arguments : length, []
是一个伪数组,只有数组的基本功能
接受从外面传来的参数
- arguments.length
- arguments[0]
arguments[1].......
<script type="text/javascript">
function a(){
console.log(arguments.length);
console.log(arguments[2]);
}
a(1,2,3,4,123,345);
//6
//3
</script>
(2) arguments 伪数组 转化为 数组
Array.prototype.slice.call(arguments);//转换
eg:
function a(){
console.log(arguments.sort());//因为是伪数组 不能享受此功能
}
a(1,2,3,4,123,345);
Array.prototype.slice.call(arguments);//转换
<script type="text/javascript">
function a(){
var argu=Array.prototype.slice.call(arguments);
console.log(argu.sort());
console.log(argu.join());
}
a(12,345,1);//[1, 12, 345]
//"12,345,1"
</script>
(3) caller callee
caller 调用者
callee 调用自己
- callee
var sum = function(n){
if (n <= 0)
return 1;
else
return n+arguments.callee(n-1);
//return n+sum(n-1); 一样的
}
alert(sum(5));
(4) apply call
相同点:
调用不一样的对象去执行
不同点:
apply(obj,参数以数组形式);
call(obj,参数1,参数2,参数3,….,.,.,.,…);
<script type="text/javascript">
function add(x,y){
console.log(x+y);
}
function a(x,y){
add.call(this,x,y);
//add.apply(this,arguments); 一样
//this.add(x,y);一样
}
a(2,5);
</script>
Summary:
add.call(this,x,y);
add.apply(this,arguments);
this.add(x,y);