导出模块
//dialog-es5.js
function Dialog(){
this.name = "test dialog";
}
Dialog.title = "标题"
Dialog.prototype.$show = function(){
console.log("show")
}
module.exports = Dialog
/**
模块化的实现方式:闭包
伪代码:
(function(){
module.exports = exports = this = {}
function Dialog(){
this.name = "test dialog";
}
Dialog.title = "标题"
Dialog.prototype.$show = function(){
console.log("show")
}
module.exports = Dialog
return module.exports
})()
*/
使用模块
//useDialog.js
let Dialog = require("./dialog-es5.js");
let dialog = new Dialog();
dialog.$show();
Dialog.prototype.$show();
/**
伪代码:
let Dialog = (function(){
module.exports = exports = {}
function Dialog(){
this.name = "test dialog";
}
Dialog.prototype.$show = function(){
console.log("show")
}
module.exports = Dialog
return module.exports
})()
*/
/*
如果使用exports.Sialog = Dialog,则是给暴露的空对象中添加一个叫Sialog的方法
使用:
let Sialog = {
Dialog: function Dialog(){
this.name = "test dialog";
}
}
Sialog
let fu = Sialog.Dialog;
let fu2 = new fu();
console.log(fu2.name)
//console.log(new Sialog.Dialog().name)
*/