Subj : Re: Confused about enumerating To : Brian Genisio From : Brendan Eich Date : Thu Feb 19 2004 12:33 am Brian Genisio wrote: > Brendan Eich wrote: > >> See JSCLASS_NEW_ENUMERATE and read jspubtd.h for a different approach >> that does not require you to set properties at the start of the >> enumeration. However, it's always good to make what for..in >> discloses match what can be accessed on a "get by get" basis, if you >> know the ids. IOW, all enumerable properties should appear to be >> properties, even if they're reflected from private data using some >> kind of shared (JSPROP_SHARED) prototype getter, e.g. >> >> /be > > > Wonderful... using JSCLASS_NEW_ENUMERATE works like a charm... except > when I return a property name that already exists. > > See, the object is much like an array, so the length property is > defined with the object, so myCollection.length is retrieved correctly. > > If, I enumerate the values of myCollection, the first thing I want to > return is the length... so I do the following: > > JS_ValueToId(cx, STRING_TO_JSVAL(JS_InternString(cx, "length")), idp); > > In this case, the for..in loop gets an empty string. What exactly do you mean? Show a testcase and its input and output, please. > If instead, I set the jsid with a slightly different spelling: > > JS_ValueToId(cx, STRING_TO_JSVAL(JS_InternString(cx, "Length")), idp); > > It works fine. That difference does not account for what is going on. Are you defining the length property on the prototype (via JS_InitClass)? If so, the for..in code will not find it if there is a length property in the original object (the one being enumerated). The for..in code suppresses ids of shadowed properties in prototypes. If the original object's length property names an empty string value, that might explain what you are seeing -- but you have to show a test case and its input and output. Code excerpts from your embedding might be helpful too. /be .