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
,但没有运气……
原文由 new_perl 发布,翻译遵循 CC BY-SA 4.0 许可协议
Since
this
keyword referes toThisBinding
in aLexicalEnvironment
, and javascript (or ECMAScript) doesn’t allow programmatic access toLexicalEnvironment
(in事实上,没有对整个Execution Context
) 的编程访问,所以 不可能 获得调用者的上下文。此外,当您在全局上下文中尝试
test.demo()
时,根本不应该有 调用者,也没有附加 上下文 到 调用者,这只是一个 全局代码,而不是调用上下文。