js 判断未定义字段

216 阅读1分钟

 

介绍

1.abc 要用typeof() 里边才好使

2.undefined 要加双引号

ok

代码

 var abc = {a:"ss"};
            var as = abc.aa;
              if(typeof(as) == "undefined"){
                console.log(1)
              }else{
                  console.log(2)
              }
              //显示1

 

图片