Global context inside UMD pattern

给你一囗甜甜゛ 提交于 2019-12-04 12:57:12
Tim Down

UPDATE

Apparently the following may not work in all situations in Chrome. See the comments to this answer.

Original answer

I'm using the following instead of this in my code to get the global object. It seems watertight in ECMAScript 3 and 5 environments:

(function(f) { return f("return this")(); })(Function)

This is a little indirect in an effort to appease linters, such as JSLint, that don't like use of eval and the Function constructor. If you don't care about such things (yay for you), you can use the following simpler code instead:

Function("return this")()

Background:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!