typescript - 如何在 TypeScript 中获取全局变量?
问题描述
我的意思是预设的全局变量,例如global
在 Node 和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..
}
推荐阅读
- javascript - 无法在 url 的查询参数中从 UI 传递空字符串。即使在代码中我已作为“”传递
- amazon-web-services - 禁用 Elastic Beanstalk 的 TRACE/TRACK HTTP 方法
- javascript - 如何使用自定义钩子适当地处理 React 中的字段更改事件?
- python - 带有 Python Selenium 自动化的 Spotify webplayer 帐户检查器
- javascript - 需要帮助的编码初学者
- sql - 为什么 Oracle 在与 NVL 比较的 BETWEEN 中不将 '' 视为 NULL
- drools - 我们可以在 TomcatServer 上安装 Business Central Workbench 7.36 吗?
- r - 子集数据框和创建嵌套列表
- python - Selenium-Python-IE 11 中“元素不再有效”的问题
- python - 为什么 DRF 响应中没有显示“url”