From: Thomas 'PointedEars' Lahn on
Tim Streater wrote:

> I'm using an object as an associative array.

You should not do it like that.

> It always has the property "main", and may have others.

It has or inherits other properties which are built-in. You should/must not
overwrite or shadow them. See also <http://PointedEars.de/scripts/map.js>.

> So, when I create it I might do this:
>
> var myobject = new Object ();

There should not be a space before the arguments list. Leave that style to
parenthesised operands instead.

> myobject["main"] = somevalue;

var myobject = {
main: somevalue
};

is equivalent, and safe nowadays.¹

> 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.

Why would you need to know this?

> So far all I've found to do something like:
>
> flag = false;

Have you declared `flag' a variable?

> for (i in myobject)

Have you declared `i' a variable?

> {
> 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.

See previous discussions about the caveats of for-in iteration.

> Is there a better approach?

Apparently you are looking for a problem to your solution.


PointedEars
___________
¹ <http://PointedEars.de/es-matrix>,
but note that features may be marked as safe for which insufficient
information is available (fixed in the next revision)
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16