
ES6 Maps are now on by default in Chrome 38 - viach
http://programming.com/social/posts/3957808016857761751
======
paulrouget
It's also in Firefox (since Firefox 13). Documentation:
[https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Global_Objects/Map)

------
petercooper
Or the original tweet rather than this weird scraping site:
[https://twitter.com/addyosmani/status/497441756708098048](https://twitter.com/addyosmani/status/497441756708098048)

------
jcampbell1
Using objects as a map always bothers me, for instance, someone decides their
username should be __proto__, and you end up with broken code:

    
    
        var map = { "__proto__": 1, "a":2 ,"b":3 };
        for (var key in map) if(map.hasOwnProperty(key)) {
           console.log(map[key]); 
        }

------
varikin
That looks like a cumbersome way to define a map, like small arrays within
arrays.

~~~
underwater
It's probably to get around the non-deterministic ordering of objects.

New Map({'a': 'bar', '1': 'foo'});

Would flip the order of the keys.

~~~
bryanlarsen
To be pedantic, in all modern browsers the order is deterministic. But it
certainly can be surprising.

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.

~~~
johnhenry
Do you have a source on this by chance?

------
jonpaul
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]) })

~~~
swannodette
To be fair `Object.keys()` is relatively recent and still needs to be shimmed
for IE < 9\. Even so I'd expect the new iterator based (like generators) key,
value, and entry iteration to eventually outperform the `Object.keys()`
interface that needs to return an array simply to have _something_ to iterate
over.

~~~
jonpaul
True. But anyone supporting IE < 9 I'd think would be using ES5 shim:
[https://github.com/es-shims/es5-shim](https://github.com/es-shims/es5-shim)
But maybe not?

------
pixdamix
direct link:
[https://twitter.com/addyosmani/status/497441756708098048](https://twitter.com/addyosmani/status/497441756708098048)

------
jaredmcateer
Practically a net neutral time saved in typing at the cost of readability of
the object literal. I don't think there will be many cases where I'll be using
Map over an Object literal.

~~~
masklinn
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

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

~~~
shawnz
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...](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-
map.prototype.foreach)

------
masklinn
Of course you could already use prototype-less objects as maps
(Object.create(null))

~~~
satori99
You are still forced to use strings as keys with object hashes.

Maps allows object keys, which could be really handy in many situations.

~~~
masklinn
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](https://news.ycombinator.com/item?id=8152850))

------
WorldWideWayne
Goodbye object literals too, apparently.

~~~
z1mm32m4n
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`.

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

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

~~~
shawnz
Try running this:

    
    
        Object.prototype.d = 4;
    

Now you'll get:

    
    
        1
        2
        3
        4

~~~
jvickers
If I were to run the code

    
    
        Object.prototype.d = 4;
    

I'd be asking for trouble.

~~~
shawnz
I agree, but lots of JS developers (mistakenly) think there is nothing wrong
with this:

    
    
        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.

