The syntax they have there is creating an array of arrays, and then passing that to the Map constructor, like you suspect. But it wouldn't be possible to make that syntax produce a Map directly -- if it did, how would you produce an array of arrays?
EDIT: My mistake, I misunderstood what you were saying.
You never had to use `hasOwnProperty()` to iterate a map. `Object.keys()` in conjunction with `Array.prototype.forEach()` works well for this. Example:
To be fair `Object.keys()` is relatively recent and still needs to be shimmed for IE < 9. Even so I'd expect the new iterator based (like generators) key, value, and entry iteration to eventually outperform the `Object.keys()` interface that needs to return an array simply to have something to iterate over.
Practically a net neutral time saved in typing at the cost of readability of the object literal. I don't think there will be many cases where I'll be using Map over an Object literal.
You can still iterate through all the properties of an object (the indexes associated to the code units, in case of a String) and recursively implement equality on them this way.
The fact that, up to ES5, there's been no agreement on which name to use for the equality function and no default implementation for it it's of no excuse to avoid the issue. It's their (the people who standardized it) Map type, and they should've been able to make it as useful as possible.
> You can still iterate through all the properties of an object and recursively implement equality on them this way.
Can't do that for a hashmap since the value can change (by mutating the object) the hash would have to change as well and the object would have to move around. Not a sane proposition.
It wouldn't be much different than in any other language, that can let you define a mutable hashable type that breaks the hashability/equality contracts needed by maps. Mutating things that you'd use as keys is a well known no-no, and value equality is the expected behavior among all the developers I know. So implementing this is certainly not any less sane than other tricky behaviours already present in javascript (think equality coercions) and it would give a behavior which makes real code simpler.
On top of that, the Map implementation could always call Object.freeze() on the keys that it receives
This is just a contrived example. In this situation there are a small number of keys that are all known in advance, which is exactly what object literals are for. A map would be more appropriate in situations where you have a large number of keys that are built up at runtime. Similarly, it is the latter situation where the ability to enumerate is important.
Absolutely, that was a comment on the title more than on ES6 maps (which are great news, and have plenty of "map" features which ES objects don't provide: https://news.ycombinator.com/item?id=8152850)
Thank you. Here is the advice from that page: Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.
Use objects when there is logic that operates on individual elements.
Scroll down a bit and their first example is a Map with one string key, one object key and one function key...
I understand that Objects can only have keys that can be represented as a string. I do not understand why they say to use a Map when all keys are the same type and all values are the same type. Also, how would "logic that operates on individual elements" differ between a Map and an Object?
> I do not understand why they say to use a Map when all keys are the same type and all values are the same type. Also, how would "logic that operates on individual elements" differ between a Map and an Object?
An object is for describing the different properties of one thing. For example:
See how each value is a different type of data (even though technically they are all strings)? Here, it would make no sense to enumerate over each property because they all have different meanings. Rather, you would use them individually based on what information you needed. On the other hand, a map is for describing a single property for many things:
var manufacturers = new Map([
["Altima", "Nissan"],
["Acura", "Nissan"],
["Accord", "Honda"],
["Camry", "Toyota"]
]);
Here, each key and each value are all the same type of data as one another (models and makes, respectively). Because of that, it would make sense to have the structure be enumerable. Similarly unlike an object, it wouldn't make sense to assume that a particular known key exists, e.g. with hardcoded references to certain keys.
I can potentially imagine a helper function like `mapFromObject`, but yeah I agree with you. That syntax is disheartening. Enough to make me want to keep using `hasOwnProperty`.
Someone asked for this elsewhere, it's really simple. Kinda odd that it's not in the standard, though. EDIT: It's because Maps preserve insertion order, so the standard doesn't advocate use like this.
var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
I'm not sure why you were downvoted. Yes we all understand JavaScript is prototype based and it's certainly possible for a malicious developer to muck with the prototype of your object. In the real world when you have control over the object in question (which imho is the majority of cases) there's no need to call hasOwnProperty.
I agree that it was a good question which shouldn't have been downvoted. But I don't think malicious developers are the real concern in this situation. Rather, it is just a matter of defensive programming. By not using hasOwnProperty (or better yet, ES6 maps), you are creating code that could break just by including a new dependency in the project.
Because of prototypical inheritance. If the object's prototype has properties, you may not want iterate those. Where as `key in map` will and `hasOwnProperty()` will only return true on those properties on the object.