js 检测变量是否存在

实际开发过程中,会有判断一个变量是否存在的场景 
首先想到的是

if(a==undefined){
        console.log("a is undefined")
    }else{

        console.log("a is defiend")
    }

这里会报错,有可能产生阻塞,而且不够优雅 

解决方法:

 if(typeof a!=="undefined"){

        console.log("a is undefined")
    }else{

        console.log("a is defiend")
    }

转: https://blog.csdn.net/dombreakpoint/article/details/74156950

猜你喜欢

转载自www.cnblogs.com/fps2tao/p/10486707.html
今日推荐