1. && 操作符
// 繁琐
if (this.isTrue) {
this.test();
}
// 简洁
this.isTrue && this.test();
2. || 操作符
// 繁琐
let num;
if (this.value) {
num = this.value;
} else {
num = 2;
}
// 繁琐
let num = this.value ? this.value : 2;
// 简洁
let num = this.value || 2; // 同上
3. ! 操作符
// 繁琐(无效值判断)
if (value == false) {
}
if (value == "") {
}
if (value == 0) {
}
if (value == null) {
}
if (value == undefined) {
}
// 简洁
if (!value) {
}
4. !! 操作符
// 繁琐(布尔值判断)
if (value == true) {
}
if (value == false) {
}
if (Boolean(value)) {
}
// 简洁(布尔值判断)
if (!!value) {
}
1. 希望本文能对大家有所帮助,如有错误,敬请指出
2. 原创不易,还请各位客官动动发财的小手支持一波(关注、评论、点赞、收藏)