I think it is best to define all your properties, rather than relying on it not being an error to reference an undefined property. It's good software engineering, and I think we will see Javascript tending in a direction where you have to give up this kind of dynamicity for performance anyways (IOW, it will cost you more to reference a non- existent property than a property declared with a default).
Remember also that many times you can set a default value on the class prototype and get it 'for free' in all the instances of the class. On 2006-05-12, at 09:19 PDT, Philip Romanik wrote: > Thanks Tucker! > > I have a related question. When I find object properties that are > not defined, I am giving them an initial value of null. This > eliminates the problem with undefined and allows me to do this, > > if (this.somevariable) > ... > > in the code. This makes cleaner code but it does potentially define > a lot of properties that are not often used. > > Any advice? > > Phil > > > > `in` is not yet supported in the SWF runtime. > > > > `hasOwnProperty` is supported in both runtimes. When we use `new > > Object` or literal objects to represent hash tables, best > practice is > > to use `hasOwnProperty` to detect the presence of a key in the > > table. (Consider a table that might have the keys `toString`, > > `constructor`, etc. which are inherited by all objects: only > > `hasOwnProperty` can tell if those keys are in the table or not.) > > > > So my recommendation to Phil is to use `hasOwnProperty` anywhere it > > is clear that we are using an object as a hash table and only resort > > to `[]` or `typeof` if it is not clear. > _______________________________________________ Laszlo-dev mailing list [email protected] http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
