Default value for Map
# Brendan Eich (13 years ago)
See also esdiscuss/2012-January/019723 where defaultdict was raised on es-discuss for the first time (if I'm not mistaken).
See also https://mail.mozilla.org/pipermail/es-discuss/2012-January/019723.html where defaultdict was raised on es-discuss for the first time (if I'm not mistaken). /be Yehuda Katz wrote: > 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 > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss
# Yehuda Katz (13 years ago)
Just to clarify, the existing Map has a first parameter, so there would
need to be some additional API work here (second parameter with undefined
first parameter, second class, defaultValue
setter, etc.).
Just to clarify, the existing Map has a first parameter, so there would need to be some additional API work here (second parameter with undefined first parameter, second class, `defaultValue` setter, etc.). On Wed, Nov 28, 2012 at 1:45 PM, Brendan Eich <brendan at mozilla.org> wrote: > See also https://mail.mozilla.org/**pipermail/es-discuss/2012-** > January/019723.html<https://mail.mozilla.org/pipermail/es-discuss/2012-January/019723.html>where defaultdict was raised on es-discuss for the first time (if I'm not > mistaken). > > /be > > Yehuda Katz wrote: > >> 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 >> ______________________________**_________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss> >> > -- Yehuda Katz (ph) 718.877.1325 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121128/0313c94e/attachment.html>
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); }
} }