2

Possible Duplicate:
is object empty?

So let's var someObject = { }; how can i detect that object hasnt any properties and methods?

0

1 Answer 1

5
function isEmpty(ob){
   for(var i in ob){ return false; }
   return true;
}

isEmpty({a:1}) // false
isEmpty({}) // true
Sign up to request clarification or add additional context in comments.

4 Comments

That won't work if someone has added a method to object.prototype. Read here yuiblog.com/blog/2006/09/26/for-in-intrigue
@xanatos: If "someone" adds to Object.prototype, you track it down, remove the offending code, then remove the offending developer.
@Ӫ_._Ӫ In the real world, it is possible to have object.prototype extensions. There were even some quite famous libraries that did it (but I don't remember which ones were).... mmmmh an example in this bug report bugs.jquery.com/ticket/1529 and even older versions of the prototype library
@xanatos: That jQuery bug is a great example. You're probably aware that the json2 library (which was the cause of that bug) no longer extends Object.prototype as the original did. Same with prototypejs. They both removed the offending code. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.