Javascript判断对象为空对象的方法

  1. for in 循环判断
    var obj = {};
    var result = function() {
    for(var key in obj) {
    return false;
    }
    return true;
    }
  2. 将json对象转化为json字符串,再判断该字符串是否为"{}"
var data = {};
var result = (JSON.stringify(data) == "{}");
  1. jquery的isEmptyObject方法
var data = {};
var result = $.isEmptyObject(data);
  1. Object.getOwnPropertyNames()方法
var data = {};
var arr = Object.getOwnPropertyNames(data);
alert(arr.length == 0);//true
  1. ES6的Object.keys()方法
var data = {};
var arr = Object.keys(data);
alert(arr.length == 0);//true

你可能感兴趣的:(JavaScript)