[Javascript] property instead of method

Steve Clay sclay at ufl.edu
Thu May 25 09:40:57 CDT 2006


Wednesday, May 24, 2006, 6:17:30 PM, liorean wrote:
> allows getters and setters, so for gecko based browsers it's doable,
> but not for iew, op or saf.

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

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.

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

Steve
-- 
http://mrclay.org/




More information about the Javascript mailing list