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.
ES6 Maps are now on by default in Chrome 38
41–50 of 54 posts
Re: ES6 Maps are now on by default in Chrome 38
#42That looks like a cumbersome way to define a map, like small arrays within arrays.
New Map({'a': 'bar', '1': 'foo'});
Would flip the order of the keys.
Re: ES6 Maps are now on by default in Chrome 38
#43That 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.
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
#44Why 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
Object.prototype.d = 4;
I'd be asking for trouble.Re: ES6 Maps are now on by default in Chrome 38
#45Why 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.
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
#46Why 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
#47Earlier 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.
Re: ES6 Maps are now on by default in Chrome 38
#48Earlier 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.
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
#49Practically 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
m = new Map([[new String(1), 1]])
> Map { "1": 1 }
m.get("1")
> undefinedRe: ES6 Maps are now on by default in Chrome 38
#50Earlier 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