Default value for Map

Yehuda Katz wycats at gmail.com
Wed Nov 28 13:36:28 PST 2012


There are many cases involving nested data structures where the ability to
define a default value in Maps would be helpful.

Straw Man:

var map = new Map(=> [])
var arr = map.get('foo')
arr === map.get('foo') // true

This enables:

map.get('foo').push(obj)

Which would be (*very*) approximately equivalent to:

class MapWithDefault extends Map {
  constructor(default) {
    this.defaultValue = default;
    super();
  }

  get(key) {
    if (!this.contains('foo')) {
      let value = this.defaultValue(key);
      map.set(key, value);
    }

    return super(key);
  }
}

-- 
Yehuda Katz
(ph) 718.877.1325
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121128/927f5eec/attachment.html>


More information about the es-discuss mailing list