Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Is this sugared syntax really necessary? #19

Open
calebsander opened this issue Jul 24, 2018 · 3 comments
Open

Is this sugared syntax really necessary? #19

calebsander opened this issue Jul 24, 2018 · 3 comments

Comments

@calebsander
Copy link

With ES2015 array destructuring, I find that let [lastItem] = arr.slice(-1) is a nice way to access the last item. It is hard to make a mistake with this syntax, which I think defeats all the reasons given in the rationale section of the README. It also has the benefit of not referencing arr twice, which means you can use it as let [lastItem] = someComplexExpressionToComputeArr().slice(-1). Without destructuring, you can still use arr.slice(-1)[0], which is only 4 characters longer than arr.lastItem.

@ljharb
Copy link
Member

ljharb commented Jul 24, 2018

It does, however, create an intermediate array (the slice) and invoke the iterable protocol (the destructuring).

@stevenwdv
Copy link

Why not use Array.prototype.at(-1) ?

@calebsander
Copy link
Author

Why not use Array.prototype.at(-1) ?

That method didn't exist in the ECMAScript spec 4 years ago, but looks like it solves the issue pointed out above.

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

No branches or pull requests

3 participants