<em id="09ttv"></em>
    <sup id="09ttv"><pre id="09ttv"></pre></sup>
    <dd id="09ttv"></dd>

        • JavaScript判斷變量是否為空對象 {} 的幾種方法

          2018-8-20    seo達人

          如果您想訂閱本博客內容,每天自動發到您的郵箱中, 請點這里

          json對象轉化為json字符串,再判斷該字符串是否為"{}"

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

          for in 循環判斷

          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

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

          方案一:

          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

          上面兩種方案的區別就是判斷判斷空數組的方式不同。

          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為任意數字返回true的問題,所以我們還應該再用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() 方法返回一個由指定對象的所有自身屬性的屬性名(包括不可枚舉屬性但不包括Symbol值作為名稱的屬性)組成的數組。

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

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

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

          Object.keys()方法

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

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

          Object.keys() 方法存在obj=""obj=[]obj=0 以及obj為任意數字返回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

          在實際應用中,如果對象不為空,并且知道對象不為空時,某個屬性一定存在,則直接判斷這個對象的此屬性是否存在。

          藍藍設計www.sdgs6788.com )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 平面設計服務

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.sdgs6788.com

          存檔

          国产国产成人久久精品| 久久亚洲精品成人无码网站| 中文国产成人精品久久不卡| av色综合久久天堂av色综合在| 奇米综合四色77777久久| 久久精品国产99国产精偷| 久久精品国产国产精品四凭| 免费精品久久天干天干| 久久久久99精品成人片试看| 中文字幕久久欲求不满| 欧美伊人久久大香线蕉综合| 久久香蕉一级毛片| 久久久精品人妻一区二区三区蜜桃 | 国产精品女同久久久久电影院| 国产999精品久久久久久| 久久久久亚洲AV成人网人人网站 | 日韩精品无码久久一区二区三| 久久精品国产亚洲AV忘忧草18| 久久精品免费一区二区三区| 久久久SS麻豆欧美国产日韩| 狠狠色伊人久久精品综合网| 亚洲精品美女久久777777| 久久久黄片| 国产成人精品综合久久久| 99久久免费国产精精品| 久久无码高潮喷水| 久久夜色精品国产亚洲av| 66精品综合久久久久久久| www久久久天天com| 久久婷婷五月综合国产尤物app| 亚洲精品国产自在久久| 久久精品亚洲男人的天堂| 91精品日韩人妻无码久久不卡 | 久久婷婷五月综合国产尤物app| 国产精品亚洲综合专区片高清久久久| 欧美噜噜久久久XXX| 午夜不卡久久精品无码免费| 亚洲人成伊人成综合网久久久| 亚洲乱码日产精品a级毛片久久| 久久久精品久久久久久| 久久久久香蕉视频|