javascript - 如何在 define 函数中获取全局范围?

标签 javascript scope requirejs global-variables requirejs-define

你能否在使用严格模式的同时获得全局作用域并确保你可以在非窗口环境中运行。

查看这些示例:

define(['other', 'thing'], function() {
    // this === window in desktop environment
    // this === GLOBAL in node environment
});
define(['other', 'thing'], function() {
    "use strict";
    // this === undefined in desktop environment
    // this === GLOBAL in node environment
    // As of my understanding node has to be configured using `node --use_strict`
    // (http://stackoverflow.com/questions/9031888/any-way-to-force-strict-mode-in-node)
    // But that not the point.
});

有什么方法可以在 define 中获取全局变量 (window/GLOBAL)。

最佳答案

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

如果您无权访问Function,那么也试试

var Function = function(){}.constructor,
    global = Function("return this")();

关于javascript - 如何在 define 函数中获取全局范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16936900/

相关文章:

javascript - 使用javascript在html中淡化div

javascript - 在匹配正则表达式中获取多行

javascript - 光滑的 slider "position: fixed"

javascript - 如何根据作用域/闭包更新解构的 JavaScript 变量?

javascript - 您可以使用也可以访问这些私有(private)属性的函数来扩展一个可以访问私有(private)属性的对象吗?

javascript - 如何将多个json对象处理成数组

java - 这样做的正确方法是什么?

javascript - Require.js 和 twitter-bootstrap

url - 为什么 require.js 忽略配置 baseUrl 和路径

javascript - RequireJS 突然将 '.map' 添加到 javascripts 而不是 '.js'