Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] property instead of method
- From: liorean at gmail.com (liorean)
- Subject: [Javascript] property instead of method
- Date: Thu May 25 17:51:14 2006
On 25/05/06, Steve Clay <sclay@xxxxxxx> wrote: <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Defining_Getters_and_Setters> > Is there power in these beyond syntactic sugar? Yes. They allow the emulation of things like the dynamically updating length property on arrays, of browser objects that have "magic" properties (See [IE Emu]) etc. > The above URL shows how to > create a dynamic "year" property to Date, and I can see that'd be handy for > something like "myDate.year += 10" but what could you do that you couldn't > by adding custom methods getYear and setYear? Can you add functions to > existing getters/setters? I would expect them to be overridable, but I don't know how that would work on {ReadOnly} properties and the like. > Seems to me that a language feature that disguises methods as properties > just serves to obfuscate interfaces, but I guess these are already out > there: Shouldn't Element.getChildNodes be a method if hasChildNodes is? How > do you test if a property is read-only? You can at least test for the > existence of a setSomething method. Test if it's read-only, huh? Well, it's not straight forward to test for this since you might have encountered a setter so that the actual vaulue you get from reading it out differs from the value you're trying to set, it might cast to a certain type, like boolean, or it might only allow a certain set of values. var result=false, temp, newValue=/*something that should be a valid value for the property*/; try{ temp=o.property; o.property=newValue; if(o.property===newValue) result=true; }catch(err){ console.log(err); } > I'm sure "dynamic" properties do make our lives easier, it's just annoying > to have code mysteriously and quietly fail because you tried to set a > property that was read-only only in certain browsers/moon phases and the > only way to know is to break out the documentation and start browser > sniffing... Well, that's the problem with ECMAScript 3 - it doesn't allow us a sufficiently deep introspection mechanism for non-native code, and it doesn't allow us to do the same type of things in native code as non-native code can do. Getters and setters is a prime example of this. [IE Emu] <uri:http://webfx.eae.net/dhtml/ieemu/> -- David "liorean" Andersson <uri:http://liorean.web-graphics.com/>
- References:
- [Javascript] property instead of method
- From: Geoff Knutzen
- [Javascript] property instead of method
- From: liorean
- [Javascript] property instead of method
- From: Steve Clay
- [Javascript] property instead of method
- Prev by Date: [Javascript] property instead of method
- Next by Date: [Javascript] Form change
- Previous by thread: [Javascript] property instead of method
- Next by thread: [Javascript] Re: Javascript Digest, Vol 39, Issue 21
- Index(es):