admin管理员组文章数量:1332597
Please explain what hack is used here (I can see that null is passed as a context to a function returning a property of it's context. So I can't clearly understand what is actually happening here.
function getGlobal(){
return (function(){
return this.dust;
}).call(null);
}
Please explain what hack is used here (I can see that null is passed as a context to a function returning a property of it's context. So I can't clearly understand what is actually happening here.
function getGlobal(){
return (function(){
return this.dust;
}).call(null);
}
Share
asked Sep 4, 2012 at 8:01
user1645462user1645462
635 bronze badges
0
2 Answers
Reset to default 6Setting the context to null will make this
pointing to the global object. So the code provided will act as accessing the dust
property of the global object.
According to the specification of ECMA 262 v5, 10.4.3 Entering Function Code
if thisArg is null or undefined, set the ThisBinding to the global object.
see http://es5.github./#x10.4.3
The trick is to use the fact that if you don't have a receiver of the function, window
(in fact the global object of the executed script, hence the name) is used.
So this trick enables to bypass a property (dust
) defined in the nearest embedding context and use the one defined in the global object.
本文标签: javascriptnull passed as context to a function callStack Overflow
版权声明:本文标题:javascript - null passed as context to a function call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742263073a2442877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论