Javascript test ( object && object !== “null” && object !== “undefined” )

后端 未结 9 1969
闹比i
闹比i 2020-12-29 22:46

I seem to be using this test a lot

if( object && object !== \"null\" && object !== \"undefined\" ){
    doSomething();
}

on

相关标签:
9条回答
  • 2020-12-29 23:28

    If you have jQuery, you could use $.isEmptyObject().

    $.isEmptyObject(null)
    $.isEmptyObject(undefined)
    var obj = {}
    $.isEmptyObject(obj)
    

    All these calls will return true. Hope it helps

    0 讨论(0)
  • 2020-12-29 23:30
    if(!!object){
      doSomething();
    }
    
    0 讨论(0)
  • 2020-12-29 23:35

    Maybe like this:

    var myObj = {};
    var isEmptyObj = !Object.keys(myObj).length;
    
    if(isEmptyObj) {
        // true
    } else {
    
    0 讨论(0)
提交回复
热议问题