1.三元操作符

当想写if...else语句时,使用三元操作符来代替。

const x = 20;
let answer;
if (x > 10) {
    answer = 'is greater';
} else {
    answer = 'is lesser';
}

简写:

const answer = x > 10 ? 'is greater' : 'is lesser';

也可以嵌套if语句:

const big = x > 10 ? " greater 10" : x

2.短路求值简写方式

当给一个变量分配另一个值时,想确定源始值不是nullundefined或空值。可以写撰写一个多重条件的if语句。

if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
     let variable2 = variable1;
}

或者可以使用短路求值方法:

const variable2 = variable1  || 'new';

继续阅读

上一篇文章介绍了使用Teensy 3.2将摩尔斯电键变为键盘的方法。经过研究,我决定使用Arduino Leonardo实现之。相比Teensy 3.2,Arduino Leonardo成本更低,并且实现起来也较为简单。电键则采用全新产品,无需手动走线、打磨底座等,避免了使用二战时期古董带来的种种问题。

继续阅读