W3C home > Mailing lists > Public > public-webapi@w3.org > June 2007

Prototype chain for objects that implement multiple interfaces

From: Cameron McCormack <cam@mcc.id.au>
Date: Wed, 6 Jun 2007 14:36:46 +1000
To: public-webapi@w3.org
Message-ID: <20070606043646.GJ20540@arc.mcc.id.au>

Hi everyone.

What should the prototype chain for an object that implements multiple
interfaces looks like?  A single interface is simple enough:

  NodeList = { an object }
  NodeList.[[Prototype]] = Object prototype object
  NodeList.prototype = { an object }
  NodeList.prototype.[[Prototype]] = Object prototype object
  NodeList.prototype.constructor = NodeList
  NodeList.prototype.someFunction = { some function }

  nl = { an object }
  nl.[[Prototype]] = NodeList.prototype

What happens if an object implements multiple interfaces, e.g. an
Element that is also an EventTarget?  I can imagine a way to handle this
with prototypes, for example:

  Node = { an object }
  Node.[[Prototype]] = Object prototype object
  Node.prototype = { an object }
  Node.prototype.constructor = Node

  Element = { an object }
  Element.[[Prototype]] = Object prototype object
  Element.prototype = { an object }
  Element.prototype.[[Prototype]] = Node.prototype
  Element.prototype.constructor = Element

  EventTarget = { an object }
  EventTarget.[[Prototype]] = Object prototype object
  EventTarget.prototype = { an object }
  EventTarget.prototype.constructor = EventTarget

  elt = { an object }
  elt.[[Prototype]] = { an object }
  elt.[[Prototype]].[[Prototype]] = null
  elt.[[Prototype]].[[Get]](P):
      1. If O doesn’t have a property with name P, go to step 4.
      2. Get the value of the property.
      3. Return Result(2).
      4. Call the [[HasProperty]] method of the Element prototype
         object.
      5. If Result(4) is true, go to step 9.
      6. Call the [[HasProperty]] method of the EventTarget prototype
         object.
      7. If Result(6) is true, go to step 11.
      8. Return undefined.
      9. Call the [[Get]] method of the Element prototype object.
      10. Return Result(9).
      11. Call the [[Get]] method if the EventTarget prototype object.
      12. Return Result(11).

The same question could apply to multiple inheritance (which is allowed
by OMG IDL, but AFAIK is not used in any of the DOM specs).

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 (see http://mcc.id.au/2007/05/binding-tests/#t008),
presumably because all Node objects in a document also implement
EventTarget.

Any opinions on how to specify this?

Thanks,

Cameron

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au
Received on Wednesday, 6 June 2007 04:36:58 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 8 January 2008 14:18:57 GMT