Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Last active February 12, 2024 00:07
Show Gist options
  • Save yangjunjun/28c0e8b5fc1d5a96e374 to your computer and use it in GitHub Desktop.
Save yangjunjun/28c0e8b5fc1d5a96e374 to your computer and use it in GitHub Desktop.
SeaJS与RequireJS的区别
define('a', function(){
console.log('module a parse ...');
return {
a: function(){
console.log('module a execute ...');
}
}
})
define('b', function(){
console.log('module b parse ...');
return {
b: function(){
console.log('module b execute ...');
}
}
})
require(['a', 'b'], function(a, b) {
console.log('module main parse ...');
a.a();
b.b();
});
define(function(require, exports, module){
console.log('module a parse ...')
exports.a = function(){
console.log('module a execute ...');
}
})
define(function(require, exports, module){
console.log('module b parse ...')
exports.b = function(){
console.log('module b execute ...');
}
})
define(function(require, exports, module){
console.log('module main parse ...');
var a = require('a');
a.a();
var b = require('b');
b.b();
})

输出结果:

require:

module b parse ... 
module a parse ...  (模块a, b 并行加载执行 )
module main parse ... ( 主模块加载执行) 
module a execute ... 
module b execute ... 

seajs:

module main parse ... ( 主模块加载执行) 
module a parse ... ( 模块 a 加载执行) 
module a execute ... 
module b parse ...  ( 模块 b 加载执行) 
module b execute ...

总结:

AMD 是提前执行,CMD 是延迟执行。

我们先把依赖分为两种,“强依赖” —— 肯定需要 和“弱依赖” —— 可能需要。 对于强依赖,如果要性能优先,则考虑参照依赖前置的思想设计你的模块加载器,我个人也更推崇这个方案一些;如果考虑开发成本优先,则考虑按照依赖就近的思想设计你的模块加载器。 对于弱依赖,只需要将弱依赖的部分改写到回调函数内即可。

参考

  1. http://www.douban.com/note/283566440/
  2. https://www.imququ.com/post/amd-simplified-commonjs-wrapping.html
  3. http://www.zhihu.com/question/20351507
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment