javascript“使用严格”和尼克的查找全局函数

发布于 2024-12-02 18:08:07 字数 567 浏览 0 评论 0原文

所以我看到了一个函数,坦率地说,它的简单性非常漂亮,因为它允许您在匿名函数中找到全局对象(取决于当时的环境,可能不是 window );然而,当你抛出 javascripts 的“use strict”时;由于关键字“this”的评估发生变化,它崩溃了。有几种方法可以实现这一点?

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

现在,如果在“use strict”的上下文中调用它们,我们就会失去所描述的功能,是否有任何可以在 ES5 严格模式下完成的等效功能?

供参考

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

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"; mode it crumbles, due to the evaluation of the keyword 'this' changing. 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?

For reference

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美煞众生 2024-12-09 18:08:07

解决方案:

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

适用于所有浏览器、引擎、ES3、ES5、严格、嵌套范围等。

轻微的变化将通过 JSLINT:

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

讨论

请参阅 如何在 JavaScript 中获取全局对象?

Solution:

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

Works in all Browsers, Engines, ES3, ES5, strict, nested scope, etc.

A slight variation will pass JSLINT:

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

Discussion

See How to get the global object in JavaScript?

秋凉 2024-12-09 18:08:07

访问全局对象(ES5之前)

如果您需要访问全局对象而不对标识符窗口进行硬编码,则可以从任何级别的嵌套函数范围执行以下操作:

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

这样你总是可以获得全局对象,因为在调用的函数内部
作为函数(即,不作为 new 的收缩器),这应该始终指向
全局对象。

严格模式下的 ECMAScript 5 实际上不再是这种情况,
因此,当您的代码处于严格模式时,您必须采用不同的模式。

例如,
如果您正在开发一个库,您可以将库代码包装在一个立即函数中
(第 4 章中讨论)然后从全局范围传递对此的引用作为
直接函数的参数。

访问全局对象(ES5之后)

通常,全局对象作为参数传递给立即函数,因此
它可以在函数内部访问,而无需使用窗口:这种方式使得
代码在浏览器之外的环境中更具互操作性:

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

“JavaScript 模式,作者:Stoyan Stefanov
(奥莱利)。版权所有 2010 Yahoo!, Inc.,9780596806750。”

Access to the Global Object (before 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.

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.

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.

Access to the Global Object (after 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:

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

“JavaScript Patterns, by Stoyan Stefanov
(O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.”

恋你朝朝暮暮 2024-12-09 18:08:07

这是 Perfection Kills 的片段,使用全局 eval。

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

ECMA3、ECMA5、严格模式等兼容,通过 JSLint。

Here's a snippet from Perfection Kills, using global eval.

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

ECMA3, ECMA5, Strict mode, etc compatible, passes JSLint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文