是否有可能在javascript中获取调用者上下文? [英] Is it possible to get the caller context in javascript?
问题描述
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
,没有运气......
I tried arguments.callee
and arguments.callee.caller
,and no luck...
推荐答案
自此
关键字在 LexicalEnvironment
和javascript中引用 ThisBinding
或ECMAScript)不允许以编程方式访问 LexicalEnvironment
(事实上,没有编程访问整个执行上下文
) ,所以不可能来获取调用者的上下文。
Since this
keyword referes to ThisBinding
in a LexicalEnvironment
, and javascript (or ECMAScript) doesn't allow programmatic access to LexicalEnvironment
(in fact, no programmatic access to the whole Execution Context
), so it is impossible to get the context of caller.
此外,当你尝试 test.demo()时
在全球范围内,根本不应该有没有来电者,也不应该将上下文附加到来电,这个只是一个全局代码,而不是一个调用上下文。
Also, when you try test.demo()
in a global context, there should be no caller at all, neither an attached context to the caller, this is just a Global Code, not a calling context.
这篇关于是否有可能在javascript中获取调用者上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!