allen at wirfs-brock.com
Tue Jan 8 14:45:14 PST 2013
On Jan 8, 2013, at 1:23 PM, Tab Atkins Jr. wrote:
> This is a problem elsewhere, too - Web Components really wants to make
> it easy to subclass DOM objects, but we've had to twist ourselves into
> knots to do it in a way that alerts the C++ side "early enough" that
> it can create the new objects with appropriate backing C++ classes.
> It can potentially be solved by efforts like moving the DOM into JS,
> which browsers are pursuing to various degrees, but it's a hard
> problem on the impl side. Meanwhile, there is unforgeable magic
> behind DOM objects, and nothing we can really do about it.
This is solved in the current ES6 draft by splitting (within the [[Construct]] internal method) object creation in to a two step process. First it calls the @@create method of the constructor. This method is responsible for actually allocating and pre-formatting the new object. A @@create method can allocate special object representations (if it knows how) or preinitialize properties of the newly allocated object to magic values, such as a brand tag. Only after this is completed is the actual constructor code called on the newly allocated instance.
When subclassing something like Array or a DOM object you would typically not over-ride @@create but just inherit it from the "built-in" superclass. Your subclass then gets the special object representation and all the inherited special methods will work for it.
More information about the es-discuss