proposal-array-last
proposal-array-last copied to clipboard
A syntax approach (arr[^n] for arr[arr.length-n])
trafficstars
- more powerful (one syntax for many cases:
arr[^1],arr[^2], etc.. orarr[~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
lastIndexandlastItem) 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
Syntax seems like a pretty high cost for something that's only useful on arrays and arraylike objects.
We can't use arr[~n] because it's already a valid expression 😂 (~1 === -2)
Yes true, so we're left with arr[^1]
why not just use negative number arr[-1], ^n may be used as a new unary operator like ~n some day.
@Fenzland because var o = { '-1': true } and var a = []; a['-1'] = 'not the last item'; are things that can already exist.