【发布时间】:2016-07-11 04:43:42
【问题描述】:
我正在阅读来自here的代码示例
你会发现它是 javascript 中的 IIFE。在阅读了一些文档后,我现在很清楚它是什么以及它是如何以简单的方式工作的。
但是在这个脚本的最后,它通过了“this['routingConfig']”
(function(exports){
// Other codes
exports.userRoles = buildRoles(config.roles);
// Other codes and definition of buildRoles
})(typeof exports === 'undefined' ? this['routingConfig'] = {} : exports);
那么似乎我们可以在其他脚本文件中直接调用它:
var access = routingConfig.userRoles;
那么具体的用法是什么
(typeof exports === 'undefined' ? this['routingConfig'] = {} : exports);
似乎'this'关键字作为全局命名空间,添加'routingConfig'作为键值对。
【问题讨论】:
-
如果您打开控制台并输入
this,您将看到全局窗口。
标签: javascript angularjs iife