Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for..of on an array?? #12

Closed
ljharb opened this issue Mar 16, 2016 · 10 comments
Closed

for..of on an array?? #12

ljharb opened this issue Mar 16, 2016 · 10 comments

Comments

@ljharb
Copy link

ljharb commented Mar 16, 2016

Here https://github.com/leoasis/redux-immutable-state-invariant/blob/master/src/trackForMutations.js#L45 you're using for..of on Object.keys, which is an array. Why not a normal for loop? The transpiled output would be much cleaner, and would work on browsers and node versions that don't have Symbol.iterator available.

@leoasis
Copy link
Owner

leoasis commented Mar 16, 2016

Yeah, I believe you're right. I guess we just need a for .. in in the keys object and we'd get the same behavior.

@ljharb
Copy link
Author

ljharb commented Mar 17, 2016

Using for..in will iterate over the entire prototype, which is not what you want. You should use a for loop over Object.keys(keys), or wrap the for..in body in if (Object.prototype.hasOwnProperty.call(keys, key)) {

@leoasis
Copy link
Owner

leoasis commented Mar 17, 2016

Yeah, you're right (again 😛), will make that change

EDIT: Actually in that code, we're only attaching properties to an empty object literal, making for..in do what we want: https://github.com/leoasis/redux-immutable-state-invariant/blob/master/src/trackForMutations.js#L37

@ljharb
Copy link
Author

ljharb commented Mar 17, 2016

That doesn't guard against someone adding enumerable properties to Object.prototype, however. for..in is never safe without a guard.

@leoasis
Copy link
Owner

leoasis commented Mar 18, 2016

Ok, makes sense. Will change it to use a regular for with Object.keys(keys) to avoid weird bugs

@leoasis
Copy link
Owner

leoasis commented Mar 18, 2016

Actually, a simpler fix would be to just make key = Object.create(null); so that we're sure that it doesn't have anything else besides its own properties

@ljharb
Copy link
Author

ljharb commented Mar 18, 2016

Object.create(null) isn't compatible with ES3 tho, so you'd be breaking anyone that still wants to be IE 8 compatible (like Airbnb). Object.keys is definitely the best way.

@leoasis
Copy link
Owner

leoasis commented Mar 18, 2016

Object.keys is also ES5, so if you target ES3 you'll need a polyfill as well.

@ljharb
Copy link
Author

ljharb commented Mar 18, 2016

Totally! But at least that one is faithfully shimmable :-) You can also use the object-keys module if you want to obviate the need for users to polyfill.

@leoasis
Copy link
Owner

leoasis commented Mar 18, 2016

Since this is just a dev only tool, I'm not so sure we really need to support ES3, since most development environments use modern browsers. Having said that, I'll use Object.keys since it's more understandable than Object.create(null).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants