Technicalarticles
// 条件语句function test(fruit) { if (fruit == 'apple' || fruit == 'strawberry') { console.log('red'); }}
function test(fruit) { // 把条件提取到数组中 const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (redFruits.includes(fruit)) { console.log('red'); }}
如果没有提供水果,抛出错误。
如果该水果的数量大于 10,将其打印出来。
function test(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; // 条件 1:fruit 必须有值 if (fruit) { // 条件 2:必须为红色 if (redFruits.includes(fruit)) { console.log('red'); // 条件 3:必须是大量存在 if (quantity > 10) { console.log('big quantity'); } } } else { thrownewError('No fruit!'); }}// 测试结果test(null); // 报错:No fruitstest('apple'); // 打印:redtest('apple', 20); // 打印:red,big quantity
1 个 if/else 语句来筛选无效的条件
3 层 if 语句嵌套(条件 1,2 & 3)
/_ 当发现无效条件时尽早返回 _/function test(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; // 条件 1:尽早抛出错误 if (!fruit) thrownewError('No fruit!'); // 条件2:必须为红色 if (redFruits.includes(fruit)) { console.log('red'); // 条件 3:必须是大量存在 if (quantity > 10) { console.log('big quantity'); } }}
/_ 当发现无效条件时尽早返回 _/function test(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (!fruit) thrownewError('No fruit!'); // 条件 1:尽早抛出错误 if (!redFruits.includes(fruit)) return; // 条件 2:当 fruit 不是红色的时候,直接返回 console.log('red'); // 条件 3:必须是大量存在 if (quantity > 10) { console.log('big quantity'); }}
这样的代码比较简短和直白,一个嵌套的 if 使得结构更加清晰。
条件反转会导致更多的思考过程(增加认知负担)。
Avoid Else, Return Early by Tim Oxley
StackOverflow discussion on if/else coding style
function test(fruit, quantity) { if (!fruit) return; const q = quantity || 1; // 如果没有提供 quantity,默认为 1 console.log(`We have ${q}${fruit}!`);}//测试结果test('banana'); // We have 1 banana!test('apple', 2); // We have 2 apple!
function test(fruit, quantity = 1) { // 如果没有提供 quantity,默认为 1 if (!fruit) return; console.log(`We have ${quantity}${fruit}!`);}//测试结果test('banana'); // We have 1 banana!test('apple', 2); // We have 2 apple!
function test(fruit) { // 如果有值,则打印出来 if (fruit && fruit.name) { console.log (fruit.name); } else { console.log('unknown'); }}//测试结果test(undefined); // unknowntest({ }); // unknowntest({ name: 'apple', color: 'red' }); // apple
// 解构 —— 只得到 name 属性// 默认参数为空对象 {}function test({name} = {}) { console.log (name || 'unknown');}//测试结果test(undefined); // unknowntest({ }); // unknowntest({ name: 'apple', color: 'red' }); // apple
使用 Lodash get 函数
使用 Facebook 开源的 idx 库(需搭配 Babeljs)
// 使用 lodash 库提供的 _ 方法function test(fruit) { console.log(_.get(fruit, 'name', 'unknown'); // 获取属性 name 的值,如果没有,设为默认值 unknown}//测试结果test(undefined); // unknowntest({ }); // unknowntest({ name: 'apple', color: 'red' }); // apple
function test(color) { // 使用 switch case 来找到对应颜色的水果 switch (color) { case 'red': return ['apple', 'strawberry']; case 'yellow': return ['banana', 'pineapple']; case 'purple': return ['grape', 'plum']; default: return []; }}//测试结果test(null); // []test('yellow'); // ['banana', 'pineapple']
// 使用对象字面量来找到对应颜色的水果const fruitColor = { red: ['apple', 'strawberry'], yellow: ['banana', 'pineapple'], purple: ['grape', 'plum'] };function test(color) { return fruitColor[color] || [];}
// 使用 Map 来找到对应颜色的水果const fruitColor = newMap() .set('red', ['apple', 'strawberry']) .set('yellow', ['banana', 'pineapple']) .set('purple', ['grape', 'plum']);function test(color) { return fruitColor.get(color) || [];}
const fruits = [ { name: 'apple', color: 'red' }, { name: 'strawberry', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'pineapple', color: 'yellow' }, { name: 'grape', color: 'purple' }, { name: 'plum', color: 'purple' }];function test(color) { // 使用 Array filter 来找到对应颜色的水果 return fruits.filter(f => f.color == color);}
const fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'grape', color: 'purple' } ];function test() { let isAllRed = true; // 条件:所有的水果都必须是红色 for (let f of fruits) { if (!isAllRed) break; isAllRed = (f.color == 'red'); } console.log(isAllRed); // false}
const fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'grape', color: 'purple' } ];function test() { // 条件:(简短形式)所有的水果都必须是红色 const isAllRed = fruits.every(f => f.color == 'red'); console.log(isAllRed); // false}
const fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'grape', color: 'purple' }];function test() { // 条件:至少一个水果是红色的 const isAnyRed = fruits.some(f => f.color == 'red'); console.log(isAnyRed); // true}
DO U LIKE?