/* 使用策略模式、消除if else语句 * 若等级规则增加、给scoreStrategies数组添加对象即可 所有和等级相关的都会随之更改 * 食用方式: * const { level, equity} = getLevel(score) * level:等级 equity:权益 */ export const scoreStrategies = [ { minScore: 5000, level: "4" }, { minScore: 2000, level: "3" }, { minScore: 500, level: "2" }, { minScore: 100, level: "1" }, { minScore: 0, level: "0" }, ]; export const gradeEquity = function (level) { return `${level * 1 === 0 ? "无门槛" : "V" + level}`; }; export function getLevel(score) { for (let i = 0; i < scoreStrategies.length; i++) { if (score >= scoreStrategies[i].minScore) { return { level: scoreStrategies[i].level, equity: gradeEquity(`${scoreStrategies[i].level}`), }; } } console.error(`score=${score},无法确定等级`); return "无法确定等级"; /* strategyV1: function (score) { return score >= 0 && score <= 600 ? "V1" : null; }, strategyV2: function (score) { return score >= 0 && score <= 600 ? "V1" : null; }, strategyV3: function (score) { return score >= 0 && score <= 600 ? "V1" : null; }, strategyV4: function (score) { return score >= 0 && score <= 600 ? "V1" : null; }, }; */ // const levelMap = {}; // for (let strategy in scoreStrategies) { // const level = scoreStrategies(strategy)(score); // if (level) { // return level; // } // } // console.error(`score=${score},无法确定等级`); // return "无法确定等级"; }