- 写在最开始,一直想系统的学习下js的设计模式,这次拜读张容铭老师的《JavaScript设计模式》颇有感触,这本书买了之后一因为这样或那样的原因搁置了半年有余,这次趁着工作不太紧赶紧把书啃掉。
function checkName (){};
function checkEmail(){};
var checkName = function(){};
var checkEmail = function(){};
var CheckObject = {
checkName: function(){},
checkEmail: function(){}
}
var CheckObject = function(){};
CheckObject.checkName = function(){};
CheckObject.checkEmail = function(){};
var CheckObject = function(){
return {
checkName: function(){},
checkEmail: function(){}
}
}
var demo_a = CheckObject();
demo_a.checkName();
var CheckObject = function(){
this.checkName = function(){};
this.checkEmail = function(){};
}
var demo_b = new CheckObject();
demo_b.checkName();
var CheckObject = function(){};
CheckObject.prototype.checkName = function(){};
CheckObject.prototype.checkEmail = function(){};
var CheckObject = function(){};
CheckObject.prototype = {
checkName: function(){},
checkEmail: function(){}
}
var CheckObject = {
checkName: function(){
console.log('...')
return this
},
checkEmail: function(){
console.log('...')
return this
}
}
CheckObject.checkName().checkEmail();
var CheckObject = function(){};
CheckObject.prototype = {
checkName: function(){
console.log('...')
return this
},
checkEmail: function(){
console.log('...')
return this
}
}
var demo_c = new CheckObject();
demo_c.checkName().checkEmail();
Function.prototype.addMethods = function(name ,fn){
this.prototype[name] = fn;
return this
}
var Methods = function(){};
Methods.addMethods('checkName', function(){return this}).addMethods('checkEmail', function(){return this})
var demo_d = new Methods();
demo_d.checkName().checkEmail();
Function.prototype.addMethods = function(name ,fn){
if(typeof(name) == 'string'){
this.prototype[name] = fn;
}else{
name.map((item,index)=>{
this.prototype[item] = fn[index];
})
}
return this
}
var Methods = function(){},
checkName = function(){console.log(1) ;return this},
checkEmail = function(){console.log(2) ;return this}
Methods.addMethods(['checkName','checkEmail'], [checkName,checkEmail])
var demo_e = new Methods();
demo_e.checkName().checkEmail();