New Array Functions

# Sebastian Malton (7 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20170711/16451ca8/attachment

# T.J. Crowder (7 years ago)

~ has a meaning: www.ecma-international.org/ecma-262/8.0/index.html#sec-bitwise-not-operator ~0 is -1. x[-1] is accessing the property called "-1" in the array object. (Which it probably doesn't have, but certainly could have.)

If you want to propose something else, you'll need to provide some means of differentiating from existing syntax, since of course breaking existing syntax is not going to happen.

-- T.J. Crowder

# Guylian Cox (7 years ago)

Unfortunately, what you're proposing is already valid and has a different behavior:

> x = [1, 2, 3, 4, 5];
[ 1, 2, 3, 4, 5 ]
> x[-1] = 'oh no'
'oh no'
> x[~0]
'oh no'
> x[4]

5

What about adding a prototype method like last to Array instead? With [1,2,3].last() returning 3, [1,2,3].last(1) returning 2, etc

Le mar. 11 juil. 2017 à 19:19, Sebastian Malton <sebastian at malton.name> a écrit :

# Alexander Jones (7 years ago)

array.get(-1) could have the desired semantics (and be nicely symmetric with Map), but actually in other languages (e.g. Python) I find that this "trick" with negative numbers can lead to a surprising (and scary) propagation of bugs, where array bounds checking is not correctly done. Actually this exact behaviour caught me^Wsomeone who isn't me out, in Immutable.js's List type already.