Live data from Hacker News

ES6 Maps are now on by default in Chrome 38

programming.com

21–30 of 54 posts

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

#24
post #14

You never had to use `hasOwnProperty()` to iterate a map. `Object.keys()` in conjunction with `Array.prototype.forEach()` works well for this. Example: var map = {a: '1', b: '2'} Object.keys(map).forEach(function(k) { console.log(map[k]) })

To be fair `Object.keys()` is relatively recent and still needs to be shimmed for IE something to iterate over.

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

#25

Earlier quoted context omitted.

Maps have quite a few advantages over bare objects: * 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

I've searched but can't find the answer... Do they maintain insertion order?

Yep. In the ES6 draft:

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

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

#27

Earlier quoted context omitted.

Maps have quite a few advantages over bare objects: * 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

I've searched but can't find the answer... Do they maintain insertion order?

They do, which is why you have to give it an iterator.

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

#28
post #14

You never had to use `hasOwnProperty()` to iterate a map. `Object.keys()` in conjunction with `Array.prototype.forEach()` works well for this. Example: var map = {a: '1', b: '2'} Object.keys(map).forEach(function(k) { console.log(map[k]) })

To be fair `Object.keys()` is relatively recent and still needs to be shimmed for IE something to iterate over.

True. But anyone supporting IE https://github.com/es-shims/es5-shim But maybe not?

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

#29
post #21

Goodbye object literals too, apparently.

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

Post reply on HN