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

A syntax approach (arr[^n] for arr[arr.length-n]) #28

Open
caub opened this issue Aug 21, 2019 · 5 comments
Open

A syntax approach (arr[^n] for arr[arr.length-n]) #28

caub opened this issue Aug 21, 2019 · 5 comments

Comments

@caub
Copy link

caub commented Aug 21, 2019

  • more powerful (one syntax for many cases: arr[^1], arr[^2], etc.. or arr[~1] if this symbol is chosen)
  • more portable (it could apply to strings, we could use or not a protocol Symbol.index, I'm not sure it's needed, less spec additions is better)
  • more readable (similar to git shortcuts, C# also has a similar syntax, etc.)
  • no web compat risks
  • less complexity added (this proposal adds lastIndex and lastItem) and prototypes 'pollution', so
    a symtax approach is more future-proof in that aspect maybe?
  • goes well with another stage1 proposal slice-notation or slice-expression as it could be renamed, it was mentioned in Slice Extensibility proposal-slice-notation#19 (comment)
@ljharb
Copy link
Member

ljharb commented Aug 21, 2019

Syntax seems like a pretty high cost for something that's only useful on arrays and arraylike objects.

@caub caub changed the title A syntax approach (arr[^n] for arr[arr.length-n]) A syntax approach (arr[^n] or arr[~n] for arr[arr.length-n]) Aug 30, 2019
@hax
Copy link
Member

hax commented Oct 21, 2019

We can't use arr[~n] because it's already a valid expression 😂 (~1 === -2)

@caub
Copy link
Author

caub commented Oct 21, 2019

Yes true, so we're left with arr[^1]

@caub caub changed the title A syntax approach (arr[^n] or arr[~n] for arr[arr.length-n]) A syntax approach (arr[^n] for arr[arr.length-n]) Oct 21, 2019
@Fenzland
Copy link

why not just use negative number arr[-1], ^n may be used as a new unary operator like ~n some day.

@ljharb
Copy link
Member

ljharb commented Nov 14, 2019

@Fenzland because var o = { '-1': true } and var a = []; a['-1'] = 'not the last item'; are things that can already exist.

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

4 participants