ES6 Maps are now on by default in Chrome 38
programming.com
programming.com
var map = { "__proto__": 1, "a":2 ,"b":3 };
for (var key in map) if(map.hasOwnProperty(key)) {
console.log(map[key]);
}My bet is that the Map() is turning an array of arrays into a map. And that in the future it will convert the original syntax into a Map as well
EDIT: My mistake, I misunderstood what you were saying.
var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
Yes, I wouldn't want to change the syntax for array of arrays.
New Map({'a': 'bar', '1': 'foo'});
Would flip the order of the keys.
For those who aren't aware, browser Javascript maintains insertion order, except for numeric keys, which are in numeric order ahead of all other keys.
var map = {a: '1', b: '2'}
Object.keys(map).forEach(function(k) { console.log(map[k]) })
* they have a length
* their keys are typed, not limited to strings. Objects as keys actually works
* they can be cleared in a single call
* they can easily be iterated over keys, values or (key, value) pairs
> forEach calls callbackfn once for each key/value pair present in the map object, in key insertion order.
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ma...
m = new Map([[new String(1), 1]])
> Map { "1": 1 }
m.get("1")
> undefinedThe 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.
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.
On top of that, the Map implementation could always call Object.freeze() on the keys that it receives
Maps allows object keys, which could be really handy in many situations.
var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
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?
An object is for describing the different properties of one thing. For example:
var myCar = {
make: "Nissan",
model: "Altima",
colour: "Black"
};
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. var map = {
a: 1,
b: 2,
c: 3
}
for (var key in map) {
console.log(map[key]);
}
outputs 1
2
3
in the console, like I expect.There are cross-browser issues with Map as well.
Object.prototype.d = 4;
Now you'll get: 1
2
3
4 Object.prototype.d = 4;
I'd be asking for trouble. Object.prototype.myDumbShim = function () { ... };
Chances are good that your code will be running in the same scope as one of these people's code eventually.If there is actually an attack vector that gets closed by checking hasOwnProperty I'm interested.