W3C home > Mailing lists > Public > whatwg@whatwg.org > November 2012

Re: [whatwg] [URL] DOM API Feedback

From: Allen Wirfs-Brock <allen@wirfs-brock.com>
Date: Mon, 19 Nov 2012 10:15:08 -0800
Message-Id: <4C42AD8B-D7D5-48C6-B401-EF274BC5E67F@wirfs-brock.com>
To: David Bruant <bruant.d@gmail.com>
Cc: whatwg@lists.whatwg.org, Boris Zbarsky <bzbarsky@MIT.EDU>

On Nov 19, 2012, at 8:00 AM, David Bruant wrote:

> Le 19/11/2012 14:38, Boris Zbarsky a écrit :
>> On 11/19/12 2:36 AM, David Bruant wrote:
>>> ...
> 
>>>> I suppose the same goes for ES6 Map.
>>> As Tab suggested, a Map subclass could certainly work
>> 
>> How close are "subclasses" to not being vaporware (both in spec and implementation terms)?
> I don't understand what you mean by vaporware. ES6 Maps are a reality in Firefox and Chrome (behind a flag for the latter). Subclassing of ES6 maps will be possible as soon as ES6 classes do (which I admit are still unimplemented)... well... it's actually possible today with __proto__ I guess.

In the ES6 draft, the Map built-in constructor is explicitly defined to be "subclassable". This is done in a manner that does not require the use of ES6 class declarations.  It simply means that if an arbitrary object is initialized by calling the Map constructor (as a function) on the object then the object will have the necessary internal state to act as a map object and the built-int Map.prototype methods that depend upon such state will work with the object.

Practically it means things like this:

class SpecialMap extends Map {
   constructor(...args) {super(...args)};
   set(key,value) {
        super.set(key,value);
        DoSomethingSpecialWhenSettingAMapElement(this,key,value);
    }
}


or without class declarations

SpecialMap.prototype = Object.create(Map.prototype); 
SpecialMap.prototype.constructor = SpecialMap;
function SpecialMap() {
      Map.call(this);      // initialize new object with internal state needed to operate as a map
}
SpecialMap.prototype.set = function(key,value) {
   Map.prototype.set.call(this,key,value);
   DoSomethingSpecialWhenSettingAMapElement(this,key,value);
}

The necessary mechanisms to make this work are specified but as far as I know have not yet been implemented.  I'm hopeful that they will soon be incorporated into one or more of the experimental Map implementations. 

Allen
Received on Monday, 19 November 2012 18:16:23 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:48:11 GMT