Live data from Hacker News

ES6 Maps are now on by default in Chrome 38

programming.com

31–40 of 54 posts

Re: ES6 Maps are now on by default in Chrome 38

#31

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.

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)

Re: ES6 Maps are now on by default in Chrome 38

#32

Why 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.

There are many cases where this won't work.

Such as?

There are cross-browser issues with Map as well.

Re: ES6 Maps are now on by default in Chrome 38

#33

Why 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.

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.

Re: ES6 Maps are now on by default in Chrome 38

#35
post #32

Earlier quoted context omitted.

There are many cases where this won't work.

Such as? There are cross-browser issues with Map as well.

Look at the other comments. ES6 Map can be properly shimmed for the last 14 years of browsers.

Re: ES6 Maps are now on by default in Chrome 38

#36
post #21

Earlier 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…

> 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.

Re: ES6 Maps are now on by default in Chrome 38

#37
post #20

Earlier 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.

It's easy to do with a helper function. For example, this works:

var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));

Re: ES6 Maps are now on by default in Chrome 38

#38

Goodbye 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`.

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]]));

Re: ES6 Maps are now on by default in Chrome 38

#39
post #20

Earlier 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.

By original syntax I meant Map({a:1, b:2})

Yes, I wouldn't want to change the syntax for array of arrays.

Re: ES6 Maps are now on by default in Chrome 38

#40

Why 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.

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.
Post reply on HN