.markdown-body{word-break:break-word;line-height:1.75;font-weight:400;font-size:15px;overflow-x:hidden;color:#333}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{line-height:1.5;margin-top:35px;margin-bottom:10px;padding-bottom:5px}.markdown-body h1{font-size:30px;margin-bottom:5px}.markdown-body h2{padding-bottom:12px;font-size:24px;border-bottom:1px solid #ececec}.markdown-body h3{font-size:18px;padding-bottom:0}.markdown-body h4{font-size:16px}.markdown-body h5{font-size:15px}.markdown-body h6{margin-top:5px}.markdown-body p{line-height:inherit;margin-top:22px;margin-bottom:22px}.markdown-body img{max-width:100%}.markdown-body hr{border:none;border-top:1px solid #ddd;margin-top:32px;margin-bottom:32px}.markdown-body code{word-break:break-word;border-radius:2px;overflow-x:auto;background-color:#fff5f5;color:#ff502c;font-size:.87em;padding:.065em .4em}.markdown-body code,.markdown-body pre{font-family:Menlo,Monaco,Consolas,Courier New,monospace}.markdown-body pre{overflow:auto;position:relative;line-height:1.75}.markdown-body pre>code{font-size:12px;padding:15px 12px;margin:0;word-break:normal;display:block;overflow-x:auto;color:#333;background:#f8f8f8}.markdown-body a{text-decoration:none;color:#0269c8;border-bottom:1px solid #d1e9ff}.markdown-body a:active,.markdown-body a:hover{color:#275b8c}.markdown-body table{display:inline-block!important;font-size:12px;width:auto;max-width:100%;overflow:auto;border:1px solid #f6f6f6}.markdown-body thead{background:#f6f6f6;color:#000;text-align:left}.markdown-body tr:nth-child(2n){background-color:#fcfcfc}.markdown-body td,.markdown-body th{padding:12px 7px;line-height:24px}.markdown-body td{min-width:120px}.markdown-body blockquote{color:#666;padding:1px 23px;margin:22px 0;border-left:4px solid #cbcbcb;background-color:#f8f8f8}.markdown-body blockquote:after{display:block;content:""}.markdown-body blockquote>p{margin:10px 0}.markdown-body ol,.markdown-body ul{padding-left:28px}.markdown-body ol li,.markdown-body ul li{margin-bottom:0;list-style:inherit}.markdown-body ol li .task-list-item,.markdown-body ul li .task-list-item{list-style:none}.markdown-body ol li .task-list-item ol,.markdown-body ol li .task-list-item ul,.markdown-body ul li .task-list-item ol,.markdown-body ul li .task-list-item ul{margin-top:0}.markdown-body ol ol,.markdown-body ol ul,.markdown-body ul ol,.markdown-body ul ul{margin-top:3px}.markdown-body ol li{padding-left:6px}@media (max-width:720px){.markdown-body h1{font-size:24px}.markdown-body h2{font-size:20px}.markdown-body h3{font-size:18px}}
Write Less Do the Same
1. if多条件判断
代码语言:javascript复制// 冗余
if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {}
// 简洁
if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {}
2. if...else...
代码语言:javascript复制// 冗余
let test: boolean;
if (x > 100) {
test = true;
} else {
test = false;
}
// 简洁
let test = x > 10;
3. Null, Undefined, 空值检查
代码语言:javascript复制// 冗余
if (first !== null || first !== undefined || first !== '') {
let second = first;
}
// 简洁
let second = first || '';
4. foreach循环
代码语言:javascript复制// 冗余
for (var i = 0; i < testData.length; i )
// 简洁
for (let i in testData)
// 或
for (let i of testData)
5. 函数条件调用
代码语言:javascript复制// 冗余
function test1() {
console.log('test1');
};
function test2() {
console.log('test2');
};
var test3 = 1;
if (test3 == 1) {
test1();
} else {
test2();
}
// 简单
(test3 === 1? test1:test2)();
复制代码
6. switch条件
代码语言:javascript复制// 冗余
switch (data) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test();
break;
// so on...
}
// 简洁
var data = {
1: test1,
2: test2,
3: test
};
data[anything] && data[anything]();
7. 多行字符串
代码语言:javascript复制// 冗余
const data = 'abc abc abc abc abc abcnt'
'test test,test test test testnt'
// 简洁
const data = `abc abc abc abc abc abc
test test,test test test test`
8. 隐式返回
代码语言:javascript复制// 冗余
function getArea(diameter) {
return Math.PI * diameter
}
// 简洁
getArea = diameter => (
Math.PI * diameter;
)
复制代码
9. 重复字符串多次
代码语言:javascript复制// 冗余
let test = '';
for(let i = 0; i < 5; i ) {
test = 'test ';
}
// 简洁
'test '.repeat(5);
10. 幂乘
代码语言:javascript复制// 冗余
Math.pow(2,3);
// 简洁而
2**3 // 8
参考
- 原文 - dev.to/blessingart…
- 原文首发 - github.com/reng99/blog…
- 更多内容 - github.com/reng99/blog…