js undefined null 是否为false

你们都写过非空判断吧

      // 输入框为空不添加标签
      if (this.ruleForm.val==null || this.ruleForm.val == undefined || this.ruleForm.val=='' || this.ruleForm.val.trim().length == 0) {
        this.$message({
          message: this.$t('programModel.labelNotNull'),
          type: 'warning'
        })
        return
      }

有没有试着如下优化

      // 输入框为空不添加标签
      if (!this.ruleForm.val || this.ruleForm.val.trim().length == 0) {
        this.$message({
          message: this.$t('programModel.labelNotNull'),
          type: 'warning'
        })
        return
      }

原因如下

alert(0 == ''); //true
alert(0 == false); //true
alert(false == ''); //true
alert(null == undefined); //true

alert(!0); //true
alert(!false); //true
alert(!undefined); //true
alert(!null); //true
alert(!''); //true

猜你喜欢

转载自blog.csdn.net/u012174809/article/details/123445823