Anne van Kesteren

IE8 getters and setters

I decided to actually check the ECMAScript 3.1 draft. If you thought HTML5 was horrible, ECMAScript 3.1 is a ~3.2MiB PDF file downloadable from a wiki page. No live updates, no inline links, no chapter links, mu. Anyways, a colleague told me getters and setters were defined in section 11.1.5 so I looked there (January 12 draft). It states get and set are the keywords to be used, as in the first example of my last post. So some part of the de facto getters and setters support survives. Hurray!

__defineGetter__ and friends are not defined at all in the draft though which seems to indicate that TC39 takes a different approach to things than e.g. the HTML WG. The draft however does define a new way to do getters and setters in section 8.10.5 and as far as I can tell that is the one Internet Explorer 8 attempts to implement. I say attempt, as Internet Explorer is using getter and setter as keywords according to a document on MSDN where the draft states get and set are supposed be it.

Thus the question is, did the Microsoft engineers made a typo? Or did TC39 decide on a change the January 12 draft does not reflect?

After some more digging, the changelog in the draft says that December 22 last year the getter and setter keywords were renamed to get and set, citing issue 441, which, as it turns out, is marked invalid. I’m guessing pratapL of TC39 made a mistake in changing it everywhere rather than just in section 11.1.5, though maybe consistency was intentional and Internet Explorer needs to change.

Comments

  1. Perhaps the the ECMAScript mailing list could clarify the issue?

    Posted by -TNO- at

  2. From what I gather, pratapL is Pratap Lakshman from VJ#SDK at Microsoft.

    Posted by Laurens Holst at

  3. Travis clarified the situation in a comment on my previous post. Apparently the MSDN documentation is out of date.

    Posted by Anne van Kesteren at

  4. That officially made my day

    Posted by TNO at

  5. Here is a hopefully complete explanation:

    The keywords used to define getters/setters in the object literal syntax have been get and set since early drafts.

    At the time issue 441 was closed, and also at the time the MSDN documentation was written, the property names used in descriptor objects passed to Object.create (and in a few other places) were getter and setter. They have since been changed to get and set (which don't need to be quoted, since the get and set used in object literals are only keywords in that context).

    Issue 441 was questioning whether the object literal keywords and the Object.create/defineProperty methods of defining getters and setters should both be supported. They should, because the object literal keywords are more concise and are a de facto standard, while Object.create/defineProperty are more expressive (since the property names and attributes can be determined programmatically, and after an object is created). So it was correct to close 441 as invalid.

    The MSDN documentation uses only descriptor objects, not object literal syntax, so it was correct when it was written (but should be updated, as Travis says).

    The MSDN doc suggests that for portability, you should test at run-time whether to use Object.create/defineProperty, or __defineGetter/Setter__. But using the object literal get/set syntax is simpler and just as portable: it will work or already works in

    Falling back to __defineGetter/Setter__ would I believe only get you additional compatibility with IE8 RC1, and some very old versions of SpiderMonkey. So use the object literal syntax unless you need programmatic selection of names and attributes, or to add getters/setters to existing objects.

    TC39 does indeed take a noticeably different approach than HTML WG / WHATWG :-)

    The lack of internal links in the PDF is now issue 452; thanks for reminding me of this.

    Posted by David-Sarah Hopwood at

  6. I admire you! Thank you!

    Posted by heyuanbo at