callee
我们知道,在递归函数中可以通过arguments.callee来实现解除函数体内代码与函数名的耦合状态,但是这种方法会导致函数体内的this指向的对象被修改;
var global = this;
var sillyFunction = function (recursed) {
if (!recursed) {
return arguments.callee(true);
}
if (this !== global) {
console.log(this);
} else {
console.log("This is the global");
}
}
sillyFunction();
按照es5中对函数内this的理解,此函数是在window环境下被调用的,按理说,this的打印值应该是window,但是此段函数的执行结果却是:
这一结果中的this是在使用arguments.callee第二次运行函数被打印的,由于第二次运行函数是在arguments的调用下实现的,其this值变成的arguments。
caller
function outer(){
inner();
//其他代码
}
function inner(){
console.log(inner.caller);//也可以写为arguments.callee.caller
}
outer();//会输出outer函数的源码
caller的调用可以让我们窥见源码,不利于代码的安全性;