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

Add spread operator! #331

Open
Kreijstal opened this issue Feb 12, 2022 · 3 comments
Open

Add spread operator! #331

Kreijstal opened this issue Feb 12, 2022 · 3 comments

Comments

@Kreijstal
Copy link

Kreijstal commented Feb 12, 2022

Usually "apply" is used as a spread operator, here's an example
convert this

Array.prototype.concat.apply([1,2,3],[3,4,5])

into

[1,2,3].concat(...[3,4,5])
@nene
Copy link
Collaborator

nene commented Mar 12, 2023

This should be an addition to the arg-spread transform. The following works:

myArr.concat.apply(myArr, [1, 2, 3]);
// -->
myArr.concat(...[1, 2, 3]);

but the following does not:

Array.prototype.concat.apply(myArr, [1, 2, 3])

@Kreijstal
Copy link
Author

I suppose [].concat.apply(myArr,[1,2,3]) won't work either?

@nene
Copy link
Collaborator

nene commented Mar 13, 2023

Yeah, that too.

There is a problem though, which we need to watch out for. Sometimes apply() is used to convert array-like objects to arrays:

function foo() {
  // arguments variable
  Array.prototype.slice.apply(arguments, []);
}
// NodeList
var divs = document.getElementsByTagName("div");
Array.prototype.slice.apply(divs, []);

I'm not sure how common such cases are, as one can often just use .call() instead of .apply() and one might not even need the second argument.

However, the mere possibility of these cases would render the arg-spread transform unsafe. The arguments case we can mostly account for, but the NodeList case is not really possible to detect.

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

No branches or pull requests

2 participants