如何在 TypeScript 中获取全局变量?



我的意思是预设的全局变量,例如 Node 中的global和浏览器中的window。我怎样才能得到它们?

下划线获取全局变量,如下所示:

var root = typeof self == 'object' && self.self === self && self || typeof global == 'object' && global.global === global && global || this || {};
root._ = _;
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..
}

最新更新