简单工厂模式也称静态工厂方法 有工厂对象决定创建某个对象得实例
(比如我是印刷厂 我要印书,我只要书的内容,就可生产了)
下面以验证提示框举例
var verify = function (txt){
this.txt =txt
}
//标题
verify.prototype.titlemsg = function(txt){
this.titlemsg = txt
console.log(this.titlemsg);
return this
}
//描述
verify.prototype.msg = function(msg){
this.msg = msg
console.log(this.msg);
return this
}
let ms = new verify()
ms.titlemsg('我是标题').msg('描述')
需求是每一种场景要显示不同的对话框
//弹出窗
var config = function (){
}
config.prototype.show = function(){
//显示
}
config.prototype.hiad = function(){
//隐藏
}
//弹出窗
var alerts = function (){
}
alerts.prototype.show = function(){
//显示
}
alerts.prototype.hiad = function(){
//隐藏
}
//通知
var inform = function (){
}
inform.prototype.show = function(){
//显示
}
inform.prototype.hiad = function(){
//隐藏
}
/***********一个函数代替所需要实例的方法***********下面写法用到了对象简写********************/
// 简写 方法太多,太臃肿
var config = function (){
}
config.prototype = {
show(){},
hiad(){}
}
var alerts = function (){
}
alerts.prototype = {
show(){},
hiad(){}
}
var inform = function (){
}
inform.prototype = {
show(){},
hiad(){}
}
function pup(nam){
switch(){
case 'config'
return new config()
case 'alerts'
return new alerts()
case 'inform'
return new inform()
}
}
// 传入对应的name 返回对应的构造函数
pup()
/************使用一个类代替对应方法******************/
function creatPup(creats){
let obj = {
show(){},
hiad(){}
};
const {show,hiad} =obj
if(creats === 'show' ){
// 显示的方法
show()
}
if(creats === 'hiad' ){
// 隐藏的方法
hiad()
}
}