2 ms·
One reason to not make get() just be [] is that people wanted some methods on Map and WeakMap. Say you have a Map named 'm' and you do m.clear(). Keep in mind
by bzbarsky 13y ago
One reason to not make get() just be [] is that people wanted some methods on Map and WeakMap.
Say you have a Map named 'm' and you do m.clear(). Keep in mind that '.' is just syntactic sugar in JS, so this is by definition the same as m["clear"](). But now you have to worry about what happens if someone uses "clear" as a key for the Map. Does that make it impossible to clear the map? Impossible to retrieve the key? Reinstate the e4x behavior where x.y() and x.y.call(x) would do different things? Something else?
These concerns, which come up all the time with the use of objects as associative arrays, are why Map and WeakMap have explicit getter/setter methods. That way you can tell when you're working with the map part and when you're just treating it like an object with methods (clear, forEach, entries, keys, size, values) on it.