> No surprises, no more need to abuse objects as dictionaries.
... pretty much every single file of JS ever written is "guilty" of this. > No surprises, no more need to abuse objects as dictionaries.
... pretty much every single file of JS ever written is "guilty" of this.If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?
var dict = {};
dict["hasOwnProperty"] = true;
dict["toString"] = "Bob";
dict["valueOf"] = 7;
... then you're in for a rough time.Maps/Set/WeakMap properly support arbitrary objects as keys.
For one simple to understand != intuitive or easy to use. Chess rules are easy to understand too, you can learn all of them in an hour, but try to play the game with any quality. So this being simple to understand does not mean its simple to code around and debug.
You are also misguided in that this addition "complicates the language". It's not like adding generics or monads or some crazy new feature. It's one of the most fundamental datatypes, implemented properly for the first time.
So, instead of saying Object foo; foo.x = "a"; when you need a dict, and having to guard against all consequences, you get to use Map foo; foo.x = "a"; and there is nothing you have to think about anymore.
..will not have any of those properties. Works only in v8 though, AFAIK.
But I don't think it quite solves the problem jashkenas is talking about, which is that JavaScript automatically calls certain properties of your object behind the scenes in many situations, so you aren't completely free to use any key you want. You might be able to use a no-proto object like this as the internal datastore for a map, but by itself an object with arbitrarily assigned keys would cause a lot of problems unless you were very careful with it.
dict["valueOf"]
since that's obviously a bad idea, but you might write: dict[user_provided_string]
Since user_provided_string could be anything, you are susceptible to unexpected behavior if the string ends up being "valueOf".It would be great if the post included good examples of how these things are going to produce better code. Maybe my imagination or experience is just insufficient to see the great win here.
There are workarounds of course, but the ironic result is that when you want to use a js object as an actual hash/dictionary (something we are repeatedly told js is great at), your code devolves into a defensive programming mess, requiring constructions like Object.prototype.hasOwnProperty.apply, etc.
dict[ "_" + user_provided_string ]
then nothing can ever go wrong. This does have an annoyance around the leading underscores. But you can get around that by adding simple accessor methods. And now you have a real dictionary.But native dictionaries have the ability to store complex objects as keys. This is kind of nice. However they are likely to create traps like making these 2 different:
my_dict[5], my_dict['5']What about the key "_proto__" (forming "__proto__") [1]? That will screw up your dictionary badly.
It's not so easy. Which is why you want Harmony Maps and Sets in the first place.
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global...
Yes, let's add some arbitrary personal convention you have to follow and keep track of, because it's so much easy than a proper Map type...
But you can get around that by adding simple accessor methods. And now you have a real dictionary.
You still don't have a real dictionary, keys are only strings for example. You just have another lame-ass implementation of a dict in JS that avoids some edge cases and still falls prey to others.
However they are likely to create traps like making these 2 different: my_dict[5], my_dict['5']
Those two ARE different.