A different semantics for WeakMap#get default value

David Bruant bruant.d at gmail.com
Mon Jan 16 13:21:41 PST 2012


[Specifying the Davids]

Le 16/01/2012 21:56, Mark S. Miller a écrit :
> I like the idea of the two argument get as proposed by David [BRUANT].
> (In fact, I like it so much that I thought I'd already specified it
> that way ;).) David [HERMAN], I'm glad to see you got it right in your
> suggestion at
> <http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#alternate_spec_based_on_ff6.0a1>.
Actually, that's not what I meant.
What I meant was to replace the get that is currently where you link to with
------
get(key, defaultValue = undefined) {
      if (key !== Object(key)) {
        throw new TypeError(...); // key must be an object
      }
      const i = private(this).keys.indexOf(key);
     
      if(i < 0 && defaultValue !== undefined){
        this.set(key, defaultValue);
      }

      return private(this).values[i];
}
------

In this version, if the key is not found, the property is set to the
return value.
The return statement returns the value that is there if the key existed
in the map or the default value (which has just been assigned).

David


More information about the es-discuss mailing list