We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/* * 编写一个名为calculateBonus 的函数来计算每个人的奖金数额,需要接收两个参数: * 员工的工资数额和绩效考核等级。 */ var calculateBonus = function( performanceLevel, salary ){ // 问题是充斥着if语句 if ( performanceLevel === 'S' ){ return salary * 4; } if ( performanceLevel === 'A' ){ return salary * 3; } if ( performanceLevel === 'B' ){ return salary * 2; } }; calculateBonus( 'B', 20000 ); // 输出:40000 calculateBonus( 'S', 6000 ); // 输出:24000
var strategies = { "S": function( salary ){ return salary * 4; }, "A": function( salary ){ return salary * 3; }, "B": function( salary ){ return salary * 2; } }; var calculateBonus = function( level, salary ){ return strategies[ level ]( salary ); }; console.log( calculateBonus( 'S', 20000 ) ); // 输出:80000 console.log( calculateBonus( 'A', 10000 ) ); // 输出:30000
$(document).on("keydown", function(event) { if(event.keyCode === 13) {// 回车键 console.log(13) } else if (event.keyCode === 39) {// 向右 console.log(39) } else if(event.keyCode === 37){// 向左 console.log(37) } else if (event.keyCode === 40) { console.log(40) } });
// 一个个独立的配置项 var codeConfig = { "13": function() { console.log(13); }, "39": function() { console.log(39); }, "37": function() { console.log(37); }, "40": function() { console.log(40); } } // 这部分不需要改动,只需要改动配置项 $(document).on("keydown", function(event) { if (codeConfig[event.keyCode]) { codeConfig[event.keyCode](); } });
The text was updated successfully, but these errors were encountered:
No branches or pull requests
策略模式
缺点
分支。
系数改为5,那我们必须深入calculateBonus 函数的内部实现,这是违反开放封闭原则的。
只有复制和粘贴。
改进
实例
改进实例
The text was updated successfully, but these errors were encountered: