hax / proposal-index-from-end

A JavaScript proposal to add `a[^i]` syntax for `a[a.length - i]`
MIT License
17 stars 0 forks source link

behavior if length is NaN #1

Closed hax closed 11 months ago

hax commented 3 years ago

Basically a[^i] work for all array-likes, the only question is what if a is not an Array-like, eg. Number(a.length) is NaN.

let x = {0: 0, 1: 1, '-1': -1}
x[^1] // ?
x[^1] = '^1' // what will happen?

There are at least four choices:

A. let it be.

So x[^1] get undefined and then x become to {..., 'NaN': '^1'}

Accidently introduce a prop named 'NaN' seems not good.

B. treat it as 0

Generic methods on Array (such as push, slice) use behavior LengthOfArrayLike

So x[^1] get -1, x become to {..., '-1': '^1'}

C. throw TypeError

D. x[^1] get undefined, x[^1] = ... throw TypeError

hax commented 11 months ago

Created a more general issue: #5 . So close this.