36 points · by viach · 12y ago · 51 comments
var map = { "__proto__": 1, "a":2 ,"b":3 };
for (var key in map) if(map.hasOwnProperty(key)) {
console.log(map[key]);
}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.
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.
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
m = new Map([[new String(1), 1]])
> Map { "1": 1 }
m.get("1")
> undefined> 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...
Maps allows object keys, which could be really handy in many situations.
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 objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
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. 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.There are cross-browser issues with Map as well.
If there is actually an attack vector that gets closed by checking hasOwnProperty I'm interested.