javascript 中的 arguments,callee.caller,apply,call 区别

记录一下:

1.arguments是一个对象, 是函数的一个特性,只有在函数内才具有这个特性,在函数外部不用使用。

举例:

functiontest(){

alert(typeofarguments);//object

}

alert(typeofarguments);//undefined

2.callee,caller

callee 表示当前正在使用的函数,例如 arguments.callee 表示test

caller 表示当前函数的调用者,如果在最顶层 那么就为 null ,如 test() 为 null ;test2() 为test

functiontest(){

alert(arguments.callee);

alert(arguments.callee.caller);

}

functiontest2(){

test();

}

test();

test2();

3.apply,call

是函数原型的一个方法,调用者的类型必须是函数。官方解释:应用某一对象的一个方法,用另一个对象替换当前对象。简单的讲,就是对象置换

apply和call的区别:方法传递的参数不同

  fun.call(this, arg1,arg2,arg3) == fun.apply(this, arguments)==this.fun(arg1, arg2, arg3)