proposal-array-last icon indicating copy to clipboard operation
proposal-array-last copied to clipboard

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

Open caub opened this issue 6 years ago • 5 comments
trafficstars

  • 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 https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-523375691

caub avatar Aug 21 '19 16:08 caub

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

ljharb avatar Aug 21 '19 19:08 ljharb

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

hax avatar Oct 21 '19 06:10 hax

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

caub avatar Oct 21 '19 07:10 caub

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

Fenzland avatar Nov 14 '19 06:11 Fenzland

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

ljharb avatar Nov 14 '19 07:11 ljharb