// 简单工厂模式
function createdPop(type, text) {
var o = new Object();
o.content = text; // 共同的属性
o.show = function () {
// 共同的方法
console.log('this is common show');
};
// 差异部分
if (type === 'alert') {
console.log('this is alert');
}
if (type === 'prompt') {
console.log('this is prompt');
}
if (type === 'confirm') {
console.log('this is confirm');
}
},
// 调用函数
var userNameAlert = createdPop('alert', '用户名只能是26个字母和数字');
JavaScript设计模式(一)【简单工厂模式】
最新推荐文章于 2023-08-01 20:23:19 发布