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

No correct way to create an empty Array. #789

Closed
lukasoppermann opened this issue Feb 20, 2017 · 7 comments

Comments

@lukasoppermann
Copy link
Contributor

commented Feb 20, 2017

Hey, so I have the following issue:

[].slice.call(this.querySelectorAll(…

Gives me an error:

/Users/lukasoppermann/Code/page-sections/src/page-section-container.js
67:10 error Parsing error: Unexpected token ]

However, if I change it to:

Array().slice.call(this.querySelectorAll(…

It works, but I get this error:

/Users/lukasoppermann/Code/page-sections/src/page-section-container.js
67:9 error The array literal notation [] is preferrable

I feel like one notation should be allowed.

@lukasoppermann

This comment has been minimized.

Copy link
Contributor Author

commented Feb 20, 2017

Okay, so for my case Array.prototype.slice works, however, I am leaving the issue open, so you can consider if it is still valid. If not, feel free to close it.

@LinusU

This comment has been minimized.

Copy link
Member

commented Feb 20, 2017

How about Array.from(this.querySelectorAll(…))?

alternatively, I think that the following is allowed:

;[].slice.call(this.querySelectorAll(…))
@lukasoppermann

This comment has been minimized.

Copy link
Contributor Author

commented Feb 20, 2017

Hey @LinusU,
I had looked at Array.from already, its pretty neat, but lacks the required browser support. 😞

Your other suggestions works well. I will however stick top Array.prototype.slice as I find it more understandable.

Maybe it would be a nice idea to mention the ;[] trick in the docs? Or did I miss it?

@LinusU

This comment has been minimized.

Copy link
Member

commented Feb 20, 2017

screen shot 2017-02-20 at 11 07 46 am

It's in the documentation :)

Sticking to Array.prototype.slice sounds sane 👍

@feross feross closed this Feb 20, 2017

@lukasoppermann

This comment has been minimized.

Copy link
Contributor Author

commented Feb 20, 2017

@LinusU okay, I did not read this as "start with a semicolon" 😉. But thanks anyway.

@LinusU

This comment has been minimized.

Copy link
Member

commented Feb 20, 2017

Hehe, yeah that was actually why I didn't close it, maybe this could benefit from some clarification... On the other hand, it's not something that is that common...

@lukasoppermann

This comment has been minimized.

Copy link
Contributor Author

commented Feb 20, 2017

@LinusU I think using [] for wrapping nodeLists into arrays is still quite common, especially when you are in the browser, where .from is not an option due to compatibility.

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.