How to check if object has any properties in JavaScript?

http://stackoverflow.com/questions/2673121/how-to-check-if-object-has-any-properties-in-javascript


function isEmptyObject(obj) {
  for(var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return false;
    }
  }
  return true;
}

isEmptyObject({}); // true
isEmptyObject({foo:'bar'});  // false

你可能感兴趣的:(JavaScript)