如何检查属性的null和未定义的两个值
How to check null and undefined both values for the property?
我有两个属性,需要分别检查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变量?
相关文章:
- 在同一页面中使用两个版本的jquery将返回'未定义不是函数'
- 不能在javascript中定义两个var
- 定义两个具有相同参数的变量
- 当我尝试两个 DOM 中的 get 元素时,HTML 和 Xpath 结果未定义
- 将字符串拆分为两个变量 - 未定义不是一个函数
- jQuery按两个变量过滤,其中一个可能未定义
- Argument Controller不是一个函数,未定义-一个页面中有两个Angular应用程序
- 如何检查属性的null和未定义的两个值
- JavaScript:定义两个类之间的距离
- 为什么't两个变量都未定义
- 无法在主干视图中定义两个事件
- 如何使用AngularJs在html中定义两个模型
- Javascript:在对象调用中使用由两个表单字段值串联而成的变量作为属性,结果是未定义的
- 检查两个变量是否未定义
- 如何在指令中定义两个ng-click函数
- 用Parse JavaScript定义两个用户之间的相互关系
- Jquery两个脚本未定义
- Javascript:两个if类型的未定义语句给出不同的结果
- 路由未定义在谷歌地图API,而发现两个地方之间的持续时间
- 如何在一个变量中定义两个类