我使用的一个项目requirejs和我有2个模块:之前AMD模块添加非AMD模块
- a.js:是,我不能触摸它的代码的非AMD模块
- b.js:是我用
define()
函数编写的AMD模块。它需要a.js
才能工作。 - app.js:是同时使用
a.js
和b.js
的实际应用程序代码。
app.js看起来是这样的:
//source code for app.js
require(['a.js', 'b.js'],
function(a, b) {
a.x = 2;//this will fail because 'a' is not defined
});
现在的问题是:什么是require()
在app.js
两个模块的最简单的方法?我不能做到这一点,如:
//source code for app.js
require(['b.js', 'a.js'],
function(b) {
a.x = 2;//it works because module 'a' defines a global variable named 'a'
b.x = 2;//this will fail because module 'b' is loaded before 'a' so it doesn't work
});