JavaScript模塊管理的簡(jiǎn)單實(shí)現(xiàn)方式詳解
1. 為什么會(huì)有這個(gè)東西?
方便組織你的代碼,提高項(xiàng)目的可維護(hù)性。一個(gè)項(xiàng)目的可維護(hù)性高不高,也體現(xiàn)一個(gè)程序員的水平,在如今越來(lái)越復(fù)雜的前端項(xiàng)目,這一點(diǎn)尤為重要。
2. 為什么不用requirejs,seajs等
它們功能強(qiáng)大,但是文件體積是個(gè)問(wèn)題,此外還有就是業(yè)務(wù)有時(shí)候可能沒(méi)那么復(fù)雜,正如開(kāi)頭所說(shuō)的:keep it simple
3. 以下的實(shí)現(xiàn)從哪里來(lái)的?
這些借鑒了requirejs,seajs,commonjs等的實(shí)現(xiàn),用于真實(shí)的項(xiàng)目,穩(wěn)定運(yùn)行,效果不錯(cuò)。
4. 適用場(chǎng)景
移動(dòng)端頁(yè)面,將js注入到html頁(yè)面,這樣就不用考慮模塊加載的問(wèn)題,從而節(jié)省了很多的代碼,在實(shí)現(xiàn)上也更為的簡(jiǎn)單。
如果是多文件加載的話(huà),需要手動(dòng)執(zhí)行文件加載順序,那么其實(shí)最好用庫(kù)來(lái)進(jìn)行依賴(lài)管理會(huì)好一點(diǎn)。
實(shí)現(xiàn)1
(function(global){
var modules = {};
var define = function (id,factory) {
if(!modules[id]){
modules[id] = {
id : id,
factory : factory
};
}
};
var require = function (id) {
var module = modules[id];
if(!module){
return;
}
if(!module.exports){
module.exports = {};
module.factory.call(module.exports,require,module.exports,module);
}
return module.exports;
}
global.define = define;
global.require = require;
})(this);
使用示例
define('Hello',function(require,exports,module){
function sayHello() {
console.log('hello modules');
}
module.exports = {
sayHello : sayHello
}
});
var Hello = require('Hello');
Hello.sayHello();
實(shí)現(xiàn)2
function Module(main,factory){
var modules = {};
factory(function(id,factory){
modules[id] = {
id : id,
factory : factory,
}
});
var require = function (id) {
var module = modules[id];
if(!module){
return;
}
if(!module.exports){
module.exports = {};
module.factory.call(module.exports,require,module.exports,module);
}
return module.exports;
}
window.require = require;
return require(main);
}
使用示例
Module('main',function(define){
define('Hello',function(require,exports,module){
function sayHello () {
console.log('hello');
}
//有效的寫(xiě)法
module.exports = {
sayHello : syaHello;
}
//或者
exports.sayHello = sayHello;
});
//mian,程序入口
define('main',function(require,exports,module){
var Hello = require('Hello');
Hello.sayHello();
});
});
實(shí)現(xiàn)3
另外一種風(fēng)格的模塊管理
(function(global) {
var exports = {}; //存儲(chǔ)模塊暴露的接口
var modules = {}; //
global.define = function (id,factory) {
modules[id] = factory;
}
global.require = function (id) {
if(exports[id])return exports[id];
else return (exports = modules[id]());
}
})(this);
使用示例
define('Hello',function(require,exports,module){
function sayHello() {
console.log('hello modules');
}
//暴露的接口
return {
sayHello : sayHello
};
});
var Hello = require('Hello');
Hello.sayHello();
實(shí)踐
有了簡(jiǎn)易的模塊化管理之后,在項(xiàng)目中,我們就可以采取這樣的結(jié)構(gòu)
-- proj
-- html -- index.html -- css -- js -- common -- module1.js(通用模塊1) -- module2.js(通用模塊2) -- page -- index.js(頁(yè)面邏輯) -- lib -- moduler.js 模塊管理庫(kù)
配合前端構(gòu)建工具(wepack,grunt,gulp等等),就可以構(gòu)建一個(gè)移動(dòng)端的頁(yè)面。
總結(jié)
如今的框架非常地多,而且越做越龐大。框架通??紤]通用性,對(duì)于精益求精的項(xiàng)目來(lái)說(shuō),可能有時(shí)候也要自己動(dòng)手去實(shí)現(xiàn)一些關(guān)鍵的點(diǎn),而學(xué)習(xí)的來(lái)源就是這些牛逼的框架。
相關(guān)文章
Add a Picture to a Microsoft Word Document
Add a Picture to a Microsoft Word Document...2007-06-06
小程序最新獲取用戶(hù)昵稱(chēng)和頭像的方法總結(jié)
這篇文章主要介紹了小程序最新獲取用戶(hù)昵稱(chēng)和頭像的方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
JS實(shí)現(xiàn)對(duì)JSON數(shù)據(jù)進(jìn)行冒泡排序
JavaScript 是一種廣泛使用的腳本語(yǔ)言,JSON是一種常見(jiàn)的數(shù)據(jù)格式,這篇文章主要來(lái)探討一下如何使用 JavaScript 對(duì) JSON 數(shù)據(jù)進(jìn)行冒泡排序,感興趣的可以了解一下2023-06-06
JS實(shí)現(xiàn)超簡(jiǎn)單的鼠標(biāo)拖動(dòng)效果
這篇文章主要介紹了JS實(shí)現(xiàn)超簡(jiǎn)單的鼠標(biāo)拖動(dòng)效果,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
javascript window.onerror事件學(xué)習(xí)新收獲
javascript window.onerror事件學(xué)習(xí)新收獲...2007-11-11
原生JS操作網(wǎng)頁(yè)給p元素添加onclick事件及表格隔行變色
原生JS操作網(wǎng)頁(yè),給網(wǎng)頁(yè)中的所有p元素添加onclick事件,使一個(gè)特定的表格隔行變色等等,感興趣的朋友可以參考下2013-12-12

