javascript - null passed as context to a function call - Stack Overflow

Please explain what hack is used here (I can see that null is passed as a context to a function returni

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
Add a ment  | 

2 Answers 2

Reset to default 6

Setting 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742263073a4411270.html

相关推荐

  • javascript - null passed as context to a function call - Stack Overflow

    Please explain what hack is used here (I can see that null is passed as a context to a function returni

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信