下面垫片中的“导出”属性的用途是什么?真的需要吗?
requirejs.config({
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
}
});
我问这个问题是因为它看起来多余 - 当模块包含在依赖项列表中时,我们将再次指定导出的名称作为函数参数:
define(['backbone'], function (Backbone) {
return Backbone.Model.extend({});
});
If shim
在你的例子中没有使用那么Backbone
作为参数传入的对象将是未定义的,因为 Backbone 不符合 AMD 标准,并且不会返回供 RequireJS 使用的对象。
define(['backbone'], function (Backbone) {
// No shim? Then Backbone here is undefined as it may
// load out of order and you'll get an error when
// trying to use Model
return Backbone.Model.extend({});
});
为了提供一些上下文,我将使用 r.js 优化器吐出的代码,但我将在本示例中对其进行简化。通过阅读优化器生成的内容,它帮助我理解了它的要点。
填充后的 Backbone 有点像这样:
// Create self invoked function with the global 'this'
// passed in. Here it would be window
define("backbone", (function (global) {
// When user requires the 'backbone' module
// as a dependency, simply return them window.Backbone
// so that properites can be accessed
return function () {
return global.Backbone;
};
}(this)));
重点是当你请求一个模块时,给 RequireJS 一些东西来返回给你,并且它会确保在这样做之前先加载它。对于优化器,它只会预先嵌入库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)