6 ms·
ES6 Maps are now on by default in Chrome 38
- deleted 12y ago[deleted]
- WorldWideWayne 12y agoGoodbye object literals too, apparently.
- z1mm32m4n 12y agoI 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`.
- masklinn 12y agoThe map constructor simply takes an iterable.
- nawitus 12y agoSomeone 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]]));
- pedrow 12y agoThe 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/Reference/Global_Objects/Map#Objects_and_maps_compared https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- WorldWideWayne 12y agoThank 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?
- shawnz 12y ago> 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.
- varikin 12y agoThat looks like a cumbersome way to define a map, like small arrays within arrays.
- raverbashing 12y agoHummm 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
- shawnz 12y agoThe 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.
- nawitus 12y agoIt'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]]));
- raverbashing 12y agoBy original syntax I meant Map({a:1, b:2}) Yes, I wouldn't want to change the syntax for array of arrays.
- masklinn 12y agoIt's an array of (key, value) pairs. The Map constructor takes an iterable of pairs.
- underwater 12y agoIt's probably to get around the non-deterministic ordering of objects. New Map({'a': 'bar', '1': 'foo'}); Would flip the order of the keys.
- jaredmcateer 12y agoPractically 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.
- mendort 12y agoMaybe you can monkey patch Object.prototype to include a toMap function? That way you get nice literals and the benefits of maps.
- masklinn 12y agoMaps 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 12y agoI've searched but can't find the answer... Do they maintain insertion order?
- shawnz 12y agoYep. 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-map.prototype.foreach https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ma...
- masklinn 12y agoThey do, which is why you have to give it an iterator.
- berdario 12y agoStill, I'd like if Map worked with values m = new Map([[new String(1), 1]]) > Map { "1": 1 } m.get("1") > undefined
- pixdamix 12y agodirect link: https://twitter.com/addyosmani/status/497441756708098048 https://twitter.com/addyosmani/status/497441756708098048
- masklinn 12y agoOf course you could already use prototype-less objects as maps (Object.create(null))
- satori99 12y agoYou are still forced to use strings as keys with object hashes. Maps allows object keys, which could be really handy in many situations.
- masklinn 12y agoAbsolutely, 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)
- jonpaul 12y agoYou 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 12y agoTo 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 12y agoTrue. 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?
- petercooper 12y agoOr the original tweet rather than this weird scraping site: https://twitter.com/addyosmani/status/497441756708098048 https://twitter.com/addyosmani/status/497441756708098048
- jcampbell1 12y agoUsing 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]); }
- paulrouget 12y agoIt's also in Firefox (since Firefox 13). Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jvickers 12y agoWhy 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.
- swannodette 12y agoThere are many cases where this won't work.
- ddoolin 12y agoSuch as? There are cross-browser issues with Map as well.
- swannodette 12y agoLook at the other comments. ES6 Map can be properly shimmed for the last 14 years of browsers.
- shawnz 12y agoTry running this: Object.prototype.d = 4; Now you'll get: 1 2 3 4
- deleted 12y ago[deleted]
- jvickers 12y agoIf I were to run the code Object.prototype.d = 4; I'd be asking for trouble.
- shawnz 12y agoI 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.