1.当一个对象的内部状态发生改变时,会导致其行为的而改变,这看起来像是改变对象
对复杂的if else进行抽取
function showResult(result){
if(result ==0){
console.log(1);
}else if(result ==1){
console.log(2);
}else if(result ==2){
console.log(3);
}
}
var Resutlstate= function(){
var states = {
state0: function(){
console.log(1)
},
state1: function(){
console.log(2)
},
state2: function(){
console.log(3)
},
state3: function(){
console.log(4)
},
state4: function(){
console.log(5)
}
}
function show(result){
states['state'+ result] && states['state'+ result]();
}
return {
show:show
}
}