First time I found out that ES6 was adding maps to JavaScript it seemed odd to me, as at that time I used to think about JavaScript objects as dictionaries/maps (you can add/delete/access items in them through string keys), but this is not true (or at least it's just partially true). I've been reminded of this fact while confirming that likewise Perl Hashes can only store strings as keys (they'll stringify any object that you use as a key).
So, first of all, JavaScript objects can only use strings as keys (compare this with .Net Dictionary<K,V> or Java's Map<K,V>, where K can be any type of object). This is the main issue addressed by ES6 Maps.
Second, as nicely explained here, a normal object already contains a number of "keys" inherited from Object.prototype, so that can pose a problem if the user of the object/dictionary attempts to use those keys as normal keys. There's a simple solution for that in ES5 (explained here), use for your dictionaries Objects with a null [[Prototype]], by means of Object.create(null);
By the way, posting a link to the ES5 and ES6 (draf) specifications adds some points to anyone's geek status :-D
No comments:
Post a Comment