Live data from Hacker News

ES6 Maps are now on by default in Chrome 38

programming.com

41–50 of 54 posts

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

#41

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.

I agree that it was a good question which shouldn't have been downvoted. But I don't think malicious developers are the real concern in this situation. Rather, it is just a matter of defensive programming. By not using hasOwnProperty (or better yet, ES6 maps), you are creating code that could break just by including a new dependency in the project.

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

#43
post #4

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

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.

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.

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

#44
post #30

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.

Try running this: Object.prototype.d = 4; Now you'll get: 1 2 3 4

If I were to run the code

    Object.prototype.d = 4;
I'd be asking for trouble.

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

#45

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.

In my programming, I have not built it in a way to prevent a malicious developer from changing object prototypes.

If there is actually an attack vector that gets closed by checking hasOwnProperty I'm interested.

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

#46
post #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.

Generally I want to look at properties from the prototype chain too.

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

#47
post #41

Earlier quoted context omitted.

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.

I agree that it was a good question which shouldn't have been downvoted. But I don't think malicious developers are the real concern in this situation. Rather, it is just a matter of defensive programming. By not using hasOwnProperty (or better yet, ES6 maps), you are creating code that could break just by including a new dependency in the project.

Thank you for your support in this difficult time.

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

#48
post #30

Earlier quoted context omitted.

Try running this: Object.prototype.d = 4; Now you'll get: 1 2 3 4

If I were to run the code Object.prototype.d = 4; I'd be asking for trouble.

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.

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

#49

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.

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

Still, I'd like if Map worked with values

    m = new Map([[new String(1), 1]])
    > Map { "1": 1 }
    m.get("1")
    > undefined

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

#50

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

Still, I'd like if Map worked with values m = new Map([[new String(1), 1]]) > Map { "1": 1 } m.get("1") > undefined

I'm not sure what you'd want here, you specifically allocated a string object and since JS has no notion of object equality it can only use identity.
Post reply on HN