Properties of JavaScript objects

Posted by clothears on Stack Overflow See other posts from Stack Overflow or by clothears
Published on 2010-05-03T16:39:38Z Indexed on 2010/05/03 16:48 UTC
Read the original article Hit count: 390

Filed under:
|

In my JavaScript, I'm using an object as an associative array. It always has the property "main", and may have others. So, when I create it I might do this:

var myobject     = new Object ();
myobject["main"] = somevalue;

Other properties may be added later. Now, at some moment I need to know whether myobject has just the one property, or several, and take different actions depending (I'm only referring to properties I've created).

So far all I've found to do is something like:

flag = false;

for (i in myobject)
     {
     if  (i=="main")  continue;
     flag = true;
     break;
     }

and then branch on flag. Or:

for (i in myobject)
     {
     if  (i=="main")  continue;
     do_some_actions ();
     break;
     }

These approaches work but feel to me like I've overlooked something. Is there a better approach?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about object