① 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