js之策略模式


// 1.将策略对象的算法封装
var Strategies = {
    S: (salary) => { return salary * 4;}, 
    A: (salary) => { return salary * 3;}, 
    B: (salary) => { return salary * 2;}, 
    C: (salary) => { return salary * 1.5;} 
};

// 2.将计算请求发到策略对象
var calculateBonus = function( level, salary ) {
    return Strategies[ level ]( salary );
};

console.log( calculateBonus( 'S', 10000 ) );

你可能感兴趣的:(js之策略模式)