Of course you could already use prototype-less objects as maps (Object.create(null))
You are still forced to use strings as keys with object hashes. Maps allows object keys, which could be really handy in many situations.
ES6 Maps are now on by default in Chrome 38
31–40 of 54 posts
Re: ES6 Maps are now on by default in Chrome 38
#32Re: ES6 Maps are now on by default in Chrome 38
#33Why do you need to use .hasOwnProperty anyway? 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.
Re: ES6 Maps are now on by default in Chrome 38
#34Re: ES6 Maps are now on by default in Chrome 38
#35Re: ES6 Maps are now on by default in Chrome 38
#36Earlier quoted context omitted.
The intention is not to replace all 'map-like' uses of Object with Map. This MDN link explains clearly I think: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
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 repres…
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.Re: ES6 Maps are now on by default in Chrome 38
#37Earlier quoted context omitted.
Hummm 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
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.
var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
Re: ES6 Maps are now on by default in Chrome 38
#38Goodbye object literals too, apparently.
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`.
var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
Re: ES6 Maps are now on by default in Chrome 38
#39Earlier quoted context omitted.
Hummm 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
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.
Yes, I wouldn't want to change the syntax for array of arrays.
Re: ES6 Maps are now on by default in Chrome 38
#40Why do you need to use .hasOwnProperty anyway? 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.