[whatwg] [URL] DOM API Feedback

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Nov 19 10:15:08 PST 2012


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


More information about the whatwg mailing list