你能否在使用严格模式的同时获得全局作用域并确保你可以在非窗口环境中运行。
查看这些示例:
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/