这三个属性都要在函数执行的时候才有定义
caller
function callerTest() {
if (callerTest.caller) {
var a= callerTest.caller.toString();
console.log(a);
} else {
console.log("callerTest.caller is null");
}
}
callerTest();
(function handleCaller() {
callerTest();
}());
callerTest.caller is null
function handleCaller() {
callerTest();
}
如上代码,handleCaller函数里面调用callerTest函数时,callerTest函数的caller属性指向handleCaller函数,即被调用的函数的caller会指向调用该函数的函数对象。函数没有被其他函数调用时,它的caller属性值是null。
arguments
arguments会在函数运行的时候在函数内部生成的一个隐藏对象,未调用的时候为null, 可以用[]来对操作,获取对应的实参。但arguments不是数组,可以通过一下的代码可以验证
console.log(arguments instanceof Array); //结果false
callee
callee是arguments对象里面的一个属性,当函数被调用时,arguments.callee会指向函数的自身,跟函数的caller属性类似。