zoukankan      html  css  js  c++  java
  • 二、js前端策略者模式

    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设计模式

  • 相关阅读:
    39页第3题 求x的n次幂
    实验4-1 求花费电费的金额
    实验二利用循环计算多个圆柱体体积
    39页第一题 四则运算及其余数
    实验一计算圆的面积
    7-14
    第六章例6-3
    第六章例6-2
    第六章例6-1
    第五章例5-9
  • 原文地址:https://www.cnblogs.com/wangc04/p/12983900.html
Copyright © 2011-2022 走看看