① requirejs和seajs的区别
SeaJS对模块的态度是懒执行, 而RequireJS对模块的态度是预执行
如下模块通过SeaJS/RequireJS来加载, 执行结果会是怎样?
define(function(require, exports, mole) {
console.log('require mole: main');
var mod1 = require('./mod1');
mod1.hello();
var mod2 = require('./mod2');
mod2.hello();
return {
hello: function() {
console.log('hello main');
}
};
});
先试试SeaJS的执行结果
require mole: main
require mole: mod1
hello mod1
require mole: mod2
hello mod2
hello main
② 下面哪个跟其他的不一样 a,commonjs b,requirejs c,amd d,cmd
CMD是国内玉伯大神在开发SeaJS的时候提出来的,属于CommonJS的一种规范,此外还有AMD,其对于的框架版是RequireJS
权1、二者都是异步模块定义(Asynchronuous Mole Definition)的一个实现;
2、CMD和AMD都是CommonJS的一种规范的实现定义,RequireJS和SeaJS是对应的实践;
3、CMD和AMD的区别:CMD相当于按需加载,定义一个模块的时候不需要立即制定依赖模块,在需要的时候require就可以了,比较方便;而AMD则相反,定义模块的时候需要制定依赖模块,并以形参的方式引入factory中。
③ requirejs和seajs的区别
相同之处
RequireJS
和
SeaJS
都是模块加载器,倡导的是一种模块化开发理念,核心价值是让
JavaScript
的模块化开发变得更简单自然。
不同之处
两者的区别如下:
定位有差异。RequireJS
想成为浏览器端的模块加载器,同时也想成为
Rhino
/
Node
等环境的模块加载器。SeaJS
则专注于
Web
浏览器端,同时通过
Node
扩展的方式可以很方便跑在
Node
服务器端。
遵循的规范不同。RequireJS
遵循的是
AMD(异步模块定义)规范,SeaJS
遵循的是
CMD
(通用模块定义)规范。规范的不同,导致了两者
API
的不同。SeaJS
更简洁优雅,更贴近
CommonJS
Moles/1.1
和
Node
Moles
规范。
社区理念有差异。RequireJS
在尝试让第三方类库修改自身来支持
RequireJS,目前只有少数社区采纳。SeaJS
不强推,采用自主封装的方式来“海纳百川”,目前已有较成熟的封装策略。
代码质量有差异。RequireJS
是没有明显的
bug,SeaJS
是明显没有
bug。
对调试等的支持有差异。SeaJS
通过插件,可以实现
Fiddler
中自动映射的功能,还可以实现自动
combo
等功能,非常方便。RequireJS
无这方面的支持。
插件机制不同。RequireJS
采取的是在源码中预留接口的形式,源码中留有为插件而写的代码。SeaJS
采取的插件机制则与
JavaScript
语言以及Node
的方式一致:开放自身,让插件开发者可直接访问或修改,从而非常灵活,可以实现各种类型的插件。
还有不少细节差异就不多说了。
总之,SeaJS
从
API
到实现,都比
RequireJS
更简洁优雅。如果说
RequireJS
是
Prototype
类库的话,则
SeaJS
是jQuery
类库。
最重要的
最后,向
RequireJS
致敬!RequireJS
和
SeaJS
是好兄弟,一起努力推广模块化开发思想,这才是最重要的。
④ seajs问题加载jquery失败的问题
seajs需要改造jquery,因为原生的jquery没有提供接口给它。建议使用AMD的requireJS。
⑤ Seajs优缺点 什么是CMD 和AMD 如何使用
1.简介
Seajs,一个Web模块加载框架,追求简单、自然的代码书写和组织方式,:Sea.js 遵循 CMD 规范,模块化JS代码。依赖的自动加载、配置的简洁清晰,可以让程序员更多地专注编码。
2.优缺点
优点:
1).提高可维护性。
2).模块化编程。
3).动态加载,前端性能优化
缺点:
1).学习文档偏少且混乱,会更改团队使用JS的编写习惯,必须使用模块化编程。
2).不太适合团队目前的情况,多JS文件但少改动,动态加载优势和模块化优势不明显。
3). 需要配套使用SPM工具,JS的打包和管理工具。
2.什么是CMD 和AMD ?
异步模块定义(AMD)是Asynchronous Mole Definition的缩写,是 RequireJS 在推广过程中对模块定义的规范化产出。
通用模块定义(CMD)是Common Mole Definition的缩写,是SeaJS 在推广过程中对模块定义的规范化产出。
RequireJS 和 SeaJS 都是模块化框架的代表,AMD和CMD,是他们各自定义模块化的方式,大同小异,主要是代码风格和API不同。
3.如何使用?
代码如下:<script src="../js/examples-master/sea-moles/seajs/seajs/2.1.1/sea.js"></script>
<script>
//配置js路径
seajs.config({
alias:{
"jquery":"../examples-master/sea-moles/jquery/jquery/1.10.1/jquery.js"
}
});
//加载模块
seajs.use('../js/seajs/init',function($){
$("#test_div").click(function(){alert(1);});
});
</script>
代码如下:
//init.js
define(function(require,exports,mole){
var $ = require('jquery');
return $;
});
⑥ seajs和requiejs的区别,和用gulp打包方法
1、执烂返行顺序不同
从demo.html 引入一个入口c.js, c.js require-b.js , b.js require - a.js
代码如下:
c mole
[html] view plain print?
define(function(require, exports, mole) {
console.log("hello mole c");
require('b');
console.log("c finished");
});
b mole
[html] view plain print?
define(function(require, exports, mole) {
console.log("hello mole b")
var a = require('a');
console.log("b finished")
});
a mole
[html] view plain print?
define(function() {
console.log("hello mole a")
});
requriejs 的 html代码:
[html] view plain print?
<!doctype html>
<html lang="仔晌en">
<head>
<meta charset="UTF-8">
<title>seajs和requirejs的区别</title>
<script src="require.min.js" data-main="c.js"></script>
</head>
<body>
</body>
</html>
执行结果:
ello mole aa.js:2
hello mole bb.js:2
b finishedb.js:4
hello mole cc.js:3
c finishedc.js:7
==============================================
seajs的html代码:
[html] view plain print?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>seajs和requirejs的区别</title>
<script src="sea.2.3.js"></script>
<script>
seajs.use('./c'饥戚饥);
</script>
</head>
<body>
</body>
</html>
执行结果:
hello mole cc.js:3
hello mole bb.js:2
hello mole aa.js:2
b finishedb.js:4
c finishedc.js:7
所以总结:
seajs是从上到下执行,
requriejs是把其中require的js全部加载完了,再往下执行。·
2、依赖的加载有所不同
在define中 requriejs加载了依赖就执行;而seajs在define中只是加载不会执行(如果要执行,需要用require()方法)
案例代码:
c.js模块
[html] view plain print?
define(['b'],function(require,exports,mole){
console.log("hello mole c");
console.log("c finished");
});
b.js模块
[html] view plain print?
define(['a'],function(require,exports,mole) {
console.log("hello mole b")
console.log("b finished")
});
a.js模块
[html] view plain print?
define(['b'],function(require,exports,mole) {
console.log("hello mole a")
});
seajs和requirejs的 html代码 和 1 中一样
执行结果:
seajs执行结果:
hello mole cc.js:2
c finishedc.js:4
requiresj的执行结果:
ello mole aa.js:2
hello mole bb.js:2
b finishedb.js:4
hello mole cc.js:2
c finishedc.js:4
总结: 在define书写中A:requirejs 加载了就执行,所以requirejs是预执行(在define中预先执行所有require依赖的js),RequireJS的做法是并行加载所有依赖的模块, 并完成解析后, 再开始执行其他代码, 因此执行结果只会"停顿"1次, 完成整个过程是会比SeaJS要快. 预执行
B:seajs只是纯粹的加载依赖的文件,不执行就连console都不执行就是纯粹的“加载”,SeaJS一样是并行加载所有依赖的模块, 但不会立即执行模块, 等到真正需要(require)的时候才开始解析,
这里耗费了时间, 因为这个特例中的模块巨大, 因此造成"停顿"2次的现象, 这就是我所说的SeaJS中的"懒执行".
在2的基础上 c.js代码为
[html] view plain print?
define(function(require,exports,mole){
require('b');
require('a');
console.log("hello mole c");
console.log("c finished");
});
执行结果都是一样
hello mole bb.js:2
b finishedb.js:4
hello mole aa.js:2
hello mole cc.js:4
c finishedc.js:6
3、取别名时requirejs默认舍掉.js的后缀
4、 打包方法
gulp 打包seajs
requirejs 打包
http://blog.csdn.net/kongjiea/article/details/48316049