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

时间:2018-08-19 11:46:38

标签: typescript

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

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

var root = typeof self == 'object' && self.self === self && self || typeof global == 'object' && global.global === global && global || this || {};
root._ = _;

1 个答案:

答案 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..
 }