var test = {
demo: function(){
//get the caller context here
}
}
//when this gets called, the caller context should be window.
test.demo();
我尝试了 arguments.callee
和 arguments.callee.caller
,但没有成功...
最佳答案
由于 this
关键字引用 LexicalEnvironment
中的 ThisBinding
,而 javascript(或 ECMAScript)不允许以编程方式访问 LexicalEnvironment
(实际上,无法以编程方式访问整个Execution Context
),因此不可能获取调用者的上下文。
此外,当您在全局上下文中尝试 test.demo()
时,应该根本没有没有调用者,也没有附加的上下文 对于调用者,这只是一个全局代码,而不是调用上下文。
关于javascript - 是否可以在 javascript 中获取调用者上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9679053/