js模塊化編碼

var modula1 = (function() {
var a = "innerParam"; //內置參數,外部讀取不到
function f1(x) {
x += '內置函數,外部不可調用';
console.log(x);
};
var f2 = function() {
f1(a);
console.log("返回這個函數")
};
var f3 = function() {
console.log('shuibianxiexie');
}
return {
f2: f2,
f3: f3
}
})();


modula1.f2(); //調用


//實現繼承
var modula2 = (function(mode) {
var F = function(){};
F.prototype = mode;
F.prototype.constructor = F;
var fexaml = new F();
return fexaml;
})(modula1);


modula2.f4 = function(){
console.log("這是新增函數");
}
console.log(modula2);
modula2.f3();


//外部js調用時,可藉助nodeJs或者requireJs
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章