二、js前端策略者模式

2021-02-15 14:19

阅读:694

标签:this   https   apply   art   info   柯里化   inf   log   UNC   

1.定义一系列的算法

2.将这些算法封装起来

3.可以让它们相互替换

const EmployeeA = function(){}
EmployeeA.prototype.calculate = function(salary){
return salary * 4;
}
const EmployeeB = function(){}
EmployeeB.prototype.calculate = function(salary){
return salary * 3;
}
const EmployeeC = function(){}
EmployeeC.prototype.calculate = function(salary){
return salary * 2;
}
const Bounds = function(){
this.salary = null;
this.levelObj = null;
}
Bounds.prototype.setSalary = function(salary){
this.salary = salary
}
Bounds.prototype.setLevelObj = function(levelObj){
this.levelObj = levelObj
}
Bounds.prototype.getBounds = function(){
return this.levelObj.calculate(this.salary)
}
const bounds = new Bounds();
bounds.setSalary(10000)
bounds.setLevelObj(new EmployeeA())
console.info(bounds.getBounds());

 

 注1.柯里化处理

     function currying(fn){

               let arr = [];

              return function cd(){

                    if(arguments.length == 0) fn.apply(this,arr);

                   Array.prototype.push.apply(arr,[].slice.call(arguments));

                    return cd

             }

     }

 注2.https://blog.csdn.net/song_mou_xia/article/details/80763833设计模式

二、js前端策略者模式

标签:this   https   apply   art   info   柯里化   inf   log   UNC   

原文地址:https://www.cnblogs.com/wangc04/p/12983900.html


评论


亲,登录后才可以留言!