简体   繁体   English

javascript“use strict”和Nick找到全局函数

[英]javascript “use strict” and Nick's find global function

So I saw a function that was, quite frankly beautiful in its simplicity as it allowed you to find the global object ( which depending on environ at the time may NOT have been window ) while within an anonymous function; 所以我看到一个函数,它的简单性非常坦率,因为它允许你在匿名函数中找到全局对象(当时依赖于环境可能不是窗口); however when you throw javascripts' "use strict"; 但是当你抛出javascripts的“use strict”时; mode it crumbles, due to the evaluation of the keyword 'this' changing. 由于关键字'this'的变化评估,它会崩溃。 There were a few ways to accomplish this? 有几种方法可以实现这一目标?

(function () {
    var win = function () {
        return (function () {
                return this;
            }());
        };
    //win now points to the global object no matter where it is called.
}());

Now, if these are called within the context of "use strict" we lose the functionality described, is there any equivalent that can be done in ES5 strict mode? 现在,如果在“use strict”的上下文中调用它们,我们将失去所描述的功能,是否有任何可以在ES5严格模式下完成的等效操作?

For reference 以供参考

(function () {
    "use strict"
    //code here is in strict mode
}())

Access to the Global Object (before ES5) 访问全局对象(在ES5之前)

If you need to access the global object without hard-coding the identifier window, you can do the following from any level of nested function scope: 如果需要访问全局对象而不对标识符窗口进行硬编码,则可以从任何级别的嵌套函数范围执行以下操作:

var global = (function () {
    return this;
}());

This way you can always get the global object, because inside functions that were invoked as functions (that is, not as constrictors with new) this should always point to the global object. 这样你就可以随时获取全局对象,因为在作为函数调用的内部函数中(也就是说,不是作为new的限制器),这应该始终指向全局对象。

This is actually no longer the case in ECMAScript 5 in strict mode, so you have to adopt a different pattern when your code is in strict mode. 在严格模式下ECMAScript 5中实际上不再是这种情况,因此当您的代码处于严格模式时,您必须采用不同的模式。

For example, if you're developing a library, you can wrap your library code in an immediate function (discussed in Chapter 4) and then from the global scope, pass a reference to this as a parameter to your immediate function. 例如,如果您正在开发一个库,您可以将库代码包装在一个立即函数中(在第4章中讨论),然后从全局范围中将一个引用作为参数传递给您的立即函数。

Access to the Global Object (after ES5) 访问全局对象(在ES5之后)

Commonly, the global object is passed as an argument to the immediate function so that it's accessible inside of the function without having to use window: this way makes the code more interoperable in environments outside the browser: 通常,全局对象作为参数传递给immediate函数,以便它可以在函数内部访问而无需使用窗口:这种方式使代码在浏览器之外的环境中更具互操作性:

(function (global) {
    // access the global object via `global`
}(this));

“JavaScript Patterns, by Stoyan Stefanov (O'Reilly). “JavaScript模式,Stoyan Stefanov(O'Reilly)。 Copyright 2010 Yahoo!, Inc., 9780596806750.” 版权所有2010 Yahoo!,Inc.,9780596806750。“

Solution: 解:

var global = Function('return this')();

Works in all Browsers, Engines, ES3, ES5, strict, nested scope, etc. 适用于所有浏览器,引擎,ES3,ES5,严格,嵌套范围等。

A slight variation will pass JSLINT: 略有变化将通过JSLINT:

var FN = Function, global = FN('return this')();

Discussion 讨论

See How to get the global object in JavaScript? 请参见如何在JavaScript中获取全局对象?

Here's a snippet from Perfection Kills , using global eval. 这是Perfection Kills的一个片段,使用全局评估。

var root = (function () {
    return this || (0 || eval)('this');
}());

ECMA3, ECMA5, Strict mode, etc compatible, passes JSLint. ECMA3,ECMA5,严格模式等兼容,通过JSLint。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM