Subj : Re: Enumeration of extended properties of Array and Object prototypes To : netscape.public.mozilla.jseng From : Chris Zumbrunn Date : Wed Mar 24 2004 02:56 am Brendan Eich writes: > I'm asking for a real-world example where such a loop breaks because of > the user-defined prototype property. Almost all such loops that I've > seen are inspecting/debugging loops, where the added prototype > properties can be filtered out, by script or by the reader. It's not > ideal, but it's not a fatal ("will break") situation. OK, I see the proposed workarounds/solutions and I now realize why you're asking for real-world examples. It's true that the problem is limited to inspecting/debugging loops in the cases of user-defined string methods or methods for other kinds of objects that do not usually serve as collections. But everybody quickly learns to avoid adding user-defined methods for the Object or Array prototypes - because these objects are used as collections and are regularly enumerated in for/in loops. > An easy way for scripts to filter is to use Object.prototype.hasOwnProperty: .... > Besides hasOwnProperty, for Arrays you could enumerate from 0 to > length-1. While this is an acceptable workaround for inspecting/debugging loops, it can't be the solution for user-defined methods for Object and Array because almost any existing Javascript code that has been written expects for/in loops to enumerate these objects without the need for such filtering. Adding a user-defined method for Object or Array would break virtually any existing script. Georg Maass writes: > Do not prototype them to create your own variation. Derivate them to > define your own variation. I guess that would be the "correct" and "clean" solution. But I think there are legitimate benefits (convenience, streamlined code structure) that can be gained from adding user-defined methods to the standard prototypes. Brendan Eich writes: > Ideally, there'd be a direct (no prototype enumeration) for..in variant, > but I kept the original language very simple, and no one has thought to > add such a construct in ECMA. That wouldn't solve the problem because all the existing code that uses the standard for/in statement would still break as soon as a user-defined method is added to the Object or Array prototypes. One real-world example that might serve well to illustrate what I mean would be getter/setter methods for data conversion of Javascript objects (for example to and from XML). Chris .