Re: Prototype chain for objects that implement multiple interfaces

On Jun 5, 2007, at 10:15 PM, Boris Zbarsky wrote:

>
> Cameron McCormack wrote:
>> This is probably not what browsers do in practice, though.  For  
>> example,
>> Opera 9 has an addEventListener method directly on the Node interface
>> prototype object
>
> What Gecko does is to put all properties for all the interfaces an  
> object implements onto the "closest" prototype object.  So if you  
> have a <div>, HTMLDivElement.prototype will have all the properties  
> the <div> is supposed to have defined directly on it.
>
> The prototype chain in Gecko basically follows the inheritance model  
> in the DOM spec: HTMLDivElement.prototype.[[Prototype]] ===  
> HTMLElement.prototype, etc. Things like EventTarget are off to the  
> side somewhere as in your imagined way to handle it.
>
> As a result, given a <div> object (call it "div"),
>
>  div.[[Prototype]] === HTMLDivElement.prototype
>  HTMLDivElement.prototype.[[Prototype]] === HTMLElement.prototype
>  div.appendChild === HTMLDivElement.prototype.appendChild
>
> are all true, while
>
>  div.appendChild === HTMLElement.prototype.appendChild
>
> is false.

In WebKit, this would be true.

> As a result, to change what the appendChild of a <div> is you have  
> to change it on HTMLDivElement.prototype.

Generally, we make the same prototype chain you describe, but we put  
methods on the JS [[Class]] that defines them. In cases of multiple  
interfaces or multiple inheritance, we fold the set of methods into  
the next concrete class to inherit from the interfaces.

> Of course all of this is subject to change, and in fact is likely to  
> change some time in the not too distant future.
>
> To be honest, do we really think that specifying the exact prototype  
> chain is desirable?  How likely are UAs to rework the mappings  
> between their native code and ECMAScript to follow said spec?

If we thought it would improve compatibility we would be open to  
changing this for WebKit. Sites hack the prototypes often enough that  
we had to add stuff like HTMLElement.prototype to our Window object,  
originally we did not have it and the standard is pretty unclear on  
whether it should exist.

To give a specific example, it could be a problem that replacing  
HTMLElement.prototype.addEventListener will affect <div> elements in  
WebKit but not in Gecko, based on what you describe.

Regards,
Maciej

Received on Wednesday, 6 June 2007 08:12:13 UTC