首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在TypeScript中获取全局变量?

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

Stack Overflow用户
提问于 2018-08-19 19:46:38
回答 1查看 258关注 0票数 1

我指的是预置的全局变量,比如Node中的global和browser中的window。我怎样才能得到它们呢?

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

代码语言:javascript
复制
var root = typeof self == 'object' && self.self === self && self || typeof global == 'object' && global.global === global && global || this || {};
root._ = _;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 02:39:03

代码语言:javascript
复制
 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..
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51917337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档