简化逻辑

时间:2019-02-18 22:00:46

标签: javascript

我是Angular的新手,正在学习一些在线课程,我正在尝试简化以下逻辑,例如使用三元运算符等。

onClick() {
    if (this.demo === true) {
      this.test = true;
    } else {
      this.test = false;
    }
  }

1 个答案:

答案 0 :(得分:3)

您可以直接分配检查结果。

this.test = this.demo === true; // strict check for type and value  

如果this.demo是布尔值,则仅分配该值即可。

this.test = this.demo;
相关问题