angularjs – angular.isUndefined(value)和不是!(value)之间的
发布时间:2020-12-17 07:00:50 所属栏目:安全 来源:网络整理
导读:我试过了: if(angular.isUndefined(value)){ // something} 和 if(!(value)){ // something} 这两者有区别吗? 是否有一个用例来选择一个而不是另一个? 解决方法 var foo = false; if(!foo) { // will log console.log("foo is defined but false");}if(an
我试过了:
if(angular.isUndefined(value)){ // something } 和 if(!(value)){ // something } >这两者有区别吗? 解决方法var foo = false; if(!foo) { // will log console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will not log as foo is defined console.log("foo is undefined") } 另一个没有定义foo的例子 if(!foo) { // will throw exception "Uncaught ReferenceError: foo is not defined " console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will log console.log("foo is undefined") } 如此有效的angular.isUndefined(foo)除了评估之外别无他法 if(typeof foo == "undefined") 为了节省1个字符包裹是的. while!-operator检查定义的变量是否计算为false if(!foo) 是一样的 if( foo != true) 更新: 正如评论中所述,当我写“评估为假”时,会出现false null undefined NaN“”(空字符串)并且包含0 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |