tags:

views:

67

answers:

3

For example, how do I know that the myObject object literal contains two items?

var myObject = {
  item1 : "blablabla",
  item2 : "blablabla
};
A: 

If you didn't override the prototypes of myObject,

var count = 0;
for (var k in myObject) ++ count;
return count;

Otherwise, see the answer haim's link.

KennyTM
What about properties inherited from the prototype chain?
Zack Mulgrew
The `for...in` statement iterates also properties that are up in the prototype chain, I would suggest you to check if the property exists *physically* in the object by `myObject.hasOwnProperty(k);` or `Object.prototype.hasOwnProperty.call(myObject, k);` for the paranoid :)
CMS
Updated for the paranoid. :p
KennyTM
I blame Douglas Crockford and JSLint.
Zack Mulgrew
A: 
var n = 0
for (var i in o) ++n
alert(n)
jspcal