• <abbr id="chdyf"></abbr>
    <ruby id="chdyf"><acronym id="chdyf"><meter id="chdyf"></meter></acronym></ruby>
    <bdo id="chdyf"></bdo>
    <dfn id="chdyf"><menu id="chdyf"></menu></dfn>
    1. <menuitem id="chdyf"></menuitem><strong id="chdyf"><menu id="chdyf"></menu></strong>

      <rt id="chdyf"><menu id="chdyf"></menu></rt>
      成人小说一区二区三区,伊人精品成人久久综合全集观看,久久HEZYO色综合,中文字幕精品人妻熟女,影音先锋成人网站,我要看免费一级毛片,中国女人做爰A片,中文字幕av久久爽Av

      JavaScript判斷變量是否為空對(duì)象 {} 的幾種方法

      2018-8-20    seo達(dá)人

      如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里

      json對(duì)象轉(zhuǎn)化為json字符串,再判斷該字符串是否為"{}"

      var obj = {}; var b = (JSON.stringify(obj) === "{}");
      console.log(b); // true
          
      • 1
      • 2
      • 3

      for in 循環(huán)判斷

      var obj = {}; var b = function() { for(var key in obj) { return false;
          } return true;
      }
      console.log(b()); // true
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      網(wǎng)上很多都是如上的說法,但是會(huì)存在一個(gè)問題,就是如果obj=null ,obj=undefinedobj=""obj=[]obj=0 以及obj為任意數(shù)字也返回true,所以有了下面這個(gè)for in 循環(huán)判斷:

      方案一:

      var obj = {}; var b = function() { for(var key in obj) { return false;
          } if(obj === null || typeof obj !== "object" || Array.isArray(obj)){ return false;
          } return true;
      }
      console.log(b()); // true
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      方案二:

      var obj = {}; var b = function() { for(var key in obj) { return false;
          } if(obj === null || typeof obj !== "object" || Object.prototype.toString.call(obj) === "[object Array]"){ return false;
          } return true;
      }
      console.log(b()); // true
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      上面兩種方案的區(qū)別就是判斷判斷空數(shù)組的方式不同。

      jQuery的jQuery.isEmptyObject(obj)方法

      var obj = {}; var b = $.isEmptyObject(obj);
      console.log(b); // true
          
      • 1
      • 2
      • 3

      jQuery.isEmptyObject(obj) 方法依然存在obj=null ,obj=undefinedobj=""obj=[]obj=0 以及obj為任意數(shù)字返回true的問題,所以我們還應(yīng)該再用typeof 或者 $.type() 判斷一下:

      var obj = {}; var b = $.isEmptyObject(obj) && $.type(obj) === "object";
      console.log(b); // true
          
      • 1
      • 2
      • 3
      var obj = {}; var b = $.isEmptyObject(obj) && typeof obj === "object" && obj !== null && !Array.isArray(obj);
      console.log(b); // true
          
      • 1
      • 2
      • 3
      var obj = {}; var b = $.isEmptyObject(obj) && typeof obj === "object" && obj !== null && Object.prototype.toString.call(obj) !== "[object Array]";
      console.log(b); // true
          
      • 1
      • 2
      • 3

      Object.getOwnPropertyNames()方法

      Object.getOwnPropertyNames() 方法返回一個(gè)由指定對(duì)象的所有自身屬性的屬性名(包括不可枚舉屬性但不包括Symbol值作為名稱的屬性)組成的數(shù)組。

      var obj = {}; var b = !Object.getOwnPropertyNames(obj).length;
      console.log(b); // true
          
      • 1
      • 2
      • 3

      Object.getOwnPropertyNames() 方法存在obj=0 以及obj為任意數(shù)字返回true的問題,所以我們還應(yīng)該再用typeof 判斷一下:

      var obj = {}; var b = !Object.getOwnPropertyNames(obj).length && typeof obj === "object";
      console.log(b); // true
          
      • 1
      • 2
      • 3

      Object.keys()方法

      Object.keys() 方法會(huì)返回一個(gè)由一個(gè)給定對(duì)象的自身可枚舉屬性組成的數(shù)組,數(shù)組中屬性名的排列順序和使用 for...in循環(huán)遍歷該對(duì)象時(shí)返回的順序一致 。

      var obj = {}; var b = !Object.keys(obj).length;
      console.log(b); // true
          
      • 1
      • 2
      • 3

      Object.keys() 方法存在obj=""obj=[]obj=0 以及obj為任意數(shù)字返回true的問題,所以依舊需要加判斷如下:

      var obj = {}; var b = !Object.keys(obj).length && typeof obj === "object" && !Array.isArray(obj);
      console.log(b); // true
          
      • 1
      • 2
      • 3
      var obj = {}; var b = !Object.keys(obj).length && typeof obj === "object" && Object.prototype.toString.call(obj) !== "[object Array]";
      console.log(b); // true
          
      • 1
      • 2
      • 3

      在實(shí)際應(yīng)用中,如果對(duì)象不為空,并且知道對(duì)象不為空時(shí),某個(gè)屬性一定存在,則直接判斷這個(gè)對(duì)象的此屬性是否存在。

      藍(lán)藍(lán)設(shè)計(jì)m.wtxcl.cn )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 平面設(shè)計(jì)服務(wù)

      日歷

      鏈接

      個(gè)人資料

      存檔