如何检查属性的null和未定义的两个值

How to check null and undefined both values for the property?

本文关键字:未定义 两个 null 检查 何检查 属性      更新时间:2023-09-26

我有两个属性,需要分别检查null和undefined,如何在if-else语句中使用?

main.js

   var validateControlRating = function () {
            if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null || 
                $scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
                &&
               ($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
                $scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {
              $scope.caculatedRatingDiv = false;
            } else {
            $http.get('app/control/rest/calculateControlEffectiveness/' + $scope.controlProcessRatingDTO.controlDesignRatingKey + '/' + $scope.controlProcessRatingDTO.controlPerformanceRatingKey).success(function (data) {
                $scope.calcaulatedRating = data;
            }, function (error) {
                $scope.statusClass ='status invalid userErrorInfo';
                var errorMessage = error.data.errorMsg;
                if (error.data.techErrorMsg) {
                    errorMessage = error.data.techErrorMsg;
                }
                $scope.statusInfo = errorMessage;
             });
            $scope.ratingValidationMsg = '';
            $scope.ratingWinValidationClass = 'valid';
            $scope.caculatedRatingDiv = true;
            $scope.enableRatingSave = false;
        }
    };

javascript有点乏味,你必须编写每个条件,并使用括号等

if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null || 
      $scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
      &&
     ($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
      $scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {...

或者只是

if ([null, undefined].indexOf( $scope.controlProcessRatingDTO.controlPerformanceRatingKey ) === -1
    &&
    [null, undefined].indexOf( $scope.controlProcessRatingDTO.controlDesignRatingKey ) === -1) {...

我认为您需要检查此正确性,先检查undefined,然后检查null并使用&&而不是||,因为您的代码将检查未定义变量的null值,这肯定会引发异常代码:

if( typeof myVar == 'undefined' ? false: myVar )
{    // go here defined and value not null  
}

代码:

if(typeof myVar != 'undefined' && myVar)
{    // go here defined and value not null  
}

在你的代码检查将进行类似

if ((typeof $scope.controlProcessRatingDTO.controlDesignRatingKey !== undefined||
     typeof $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== undefined) &&
    ($scope.controlProcessRatingDTO.controlDesignRatingKey !== null ||
      $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== null)) {
    //  do home work
}else {  // do other home work  }

您也可以使用否定运算符,但这也适用于"false":

if (!$scope.controlProcessRatingDTO.controlPerformanceRatingKey && !$scope.controlProcessRatingDTO.controlDesignRatingKey) { 

这有点短,但如果您想单独处理False值,请使用上面的Adeneo答案。

您可以这样做:

if ( some_variable == null ){
  // some_variable is either null or undefined
}

摘自:如何在JavaScript中检查未定义或null变量?