我的意思是预设的全局变量,例如Node中的global
和浏览器中的window
。我怎样才能得到它们?
下划线获得全局变量,如下所示:
var root = typeof self == 'object' && self.self === self && self || typeof global == 'object' && global.global === global && global || this || {};
root._ = _;
答案 0 :(得分:0)
declare var global: any;
declare var window: any;
var root: any = null;
try {
root = typeof global === 'object' ? global : window;
} catch(e) {
// catch is when you are executing in custom V8 where it is
// configured to throw instead of assuming undefined..
}
if (root) {
// either global or window is present here..
}