I’m slightly baffled by Responding to Change: Updated Getter/Setter Syntax in IE8 RC 1 and not sure who is to blame or whether there is some misunderstanding. As the blog post of Travis indicates non-Internet Explorer browsers have had support for getters and setters for quite a while. JavaScript Getters and Setters elaborates on how they work and mentions they are supported in relatively recent versions of Opera, Safari, and Firefox. So Internet Explorer adding support for getters and setters sounds great initially, until you figure out they support them in a completely different and unique way, apparently as agreed upon with the other three browser vendors.
Huh? And this is somehow better for interoperability? I would love to know why TC39 made this strange design decision and why Microsoft decided to follow it in name of interoperability as it clearly leads to duplicate code short term and other browsers most likely cannot remove their “ancient” getters and setters support long term, which leads to bloat.
For example, here is something simple that works in the non-Internet Explorer browsers:
foo={get test(){ return "foo"; }};
alert(foo.test);
And here is a possible equivalent in Internet Explorer 8:
foo={};
Object.defineProperty(foo, "test", { getter: function() { return "foo" } });
alert(foo.test);
There are various syntax variations allowed, but the point is that the latter does not work in non-Internet Explorer and the former does not work in Internet Explorer 8. Yet somehow this is sold to developers as increased interoperability. Yay!
This is exactly the reason why you should invest time in learning *some* web framework.
Microsoft will never try to stop locking in people until their browser is as dead as netscape 3.0 now.
The first example (syntactical getters and setters) is part of ES3.1, and presumably IE will implement support for it at some point (they certainly appear to be strong supporters of ES3.1, since they pushed for ES3.1 in lieu of ES4). However, MS's blog post is focused on the functionality of augmenting DOM elements with getters and setters, and it's simply impossible to augment existing objects with syntactical getters and setters since they are defined in the context of object literals. I guess you could object to MS implementing the parts of ES3.1 that are "new" prior to features existing in current browsers, but they seem intent on implementing the whole spec, and that is progress. Far better than the XDomainRquest debacle. Or I think I guess you are objecting to si TC39's decision to use a new API for getters and setters. However, the Object.defineProperty was introduced for controlling the various ES property attributes (readonly, permanent, dontenum), and so it made sense to put getters and setters under this API to be consistent. Furthermore, getters and setters tend to be needed much more by library's than end users. Branching between Object.defineProperty and __defineGetter__ for a lib dev is actually pretty trivial. This is much less painful than syntactic forking, which was avoided by TC39.
I'm confused by why you would be upset at Microsoft and the IE team for agreeing to follow standards.
@Chris Wilson: If the IE team were not so selective with their standards support, perhaps we would not be upset. What happened to the whole event target implementation, svg support, the audio and video tags? Did they sleep through those standards? Why the half-assed implementation so that I have to try-catch it because it throws on non-dom nodes? As my mom used to say, "Any job worth doing is worth doing right". Perhaps I am wrong, but I thought part of the IE teams goal was to make web devs lives easier.
Kris, ok, that’s cool, but the first example does not work in Internet Explorer. They appear to be using getter
and setter
as names instead. Any ideas? It would be nice to see an overview of all the different syntaxes in use and what you can use them for. E.g. why can __defineGetter__
not be used for the DOM?
I am not sure, but I imagine they haven't implemented object literal syntax yet, and I wouldn't expect it until IE9. I was under the impression that object literal form of getters and setters should use the get and set keywords (to be compatible with existing implementations). While the Object.defineProperty mechanism for modifying existing objects would use the "getter" and "setter" property names (this not new syntax, but rather a new function that is available).
Note: The IE8 getter/setter documentation was out-of-date. The syntax in the builds is get
and set
for consistency with the object literal syntax, and we're working to update the documentation--sorry about the confusion. (ES3.1 latest drafts have get
and set
for property descriptors).
Ah, thanks Travis!