【发布时间】:2019-05-31 15:58:08
【问题描述】:
当我去绝大多数网站时,打开控制台并粘贴以下代码:
(function(global, appName, app) {
if (typeof define === "function" && typeof define.amd === "object") {
console.log("AMD/RequireJS");
define(app);
console.log("defined app");
} else if (typeof module !== "undefined") {
console.log("CommonJS");
module.exports = app(global);
console.log("defined with module.exports");
} else {
console.log("Browser");
global[appName] = app(global);
console.log("defined as global");
}
})(this, "App", function(global) {
"use strict";
console.log("defining getThis");
function getThis() {
console.log('i got it!');
}
return {
getThis: getThis
};
});
console.log("finished IIFE");
App.getThis();
我得到以下输出:
Browser
defining getThis
defined as global
finished IIFE
i got it!
如果我将相同的代码粘贴到使用 RequireJS 的站点的控制台中,例如 www.bestbuy.com 或 www.homedepot.com,它会失败并显示以下输出:
AMD/RequireJS
defined app
finished IIFE
Uncaught ReferenceError: App is not defined at <anonymous>:27:1
我尝试将define(app); 更改为define(['App'], app);,但没有任何效果。
我已经在谷歌上搜索了好几个小时了,我已经无计可施了。我认为 UMD 模式是通用的因此它的名字。什么给了?
【问题讨论】:
标签: javascript requirejs umd