tc39 / proposal-array-last

A JavaScript TC39 Proposal for getting the last element from an array
311 stars 13 forks source link

Compatibility with existing code #1

Closed kmiller68 closed 6 years ago

kmiller68 commented 6 years ago

Have you looked into the compatibility of using '-1' as a accessor to the last element of an array? My concern is that there may be code in the wild like:

let myListOfObjects = [{}, {}];

let i = myListOfObjects.length;
let v;
while (v = myListOfObjects[--i]) {
    ...
}

Which could break with your proposal.

markogresak commented 6 years ago

Also, code like this:

const array = [1, 2, 3];
const itemIndex = array.indexOf(5); // -1
if (array[itemIndex]) {
    // ...
}

Which is probably even more commonplace.

keithamus commented 6 years ago

Absolutely, I'm faily certain this will pose as a web compat issue. I wanted to float the idea regardless, as it has been mentioned before and is something at least entertaining the idea to give ratified feedback as to why it cannot be done.

keithamus commented 6 years ago

Closing this. The Readme has been updated to reflect the current state of this proposal.

john-aws commented 6 years ago

The ECMAScript standard is not exactly easy to read but I could not locate a definitive statement that 'out of bounds array access must yield undefined', or similar falsey value. Does the spec require this? If not, the examples quoted above of code that would break if this proposal were implemented seem perverse to me. Why should forward progress (the arr[-N] proposal) be off-limits because a few individuals wrote such fragile code?

ljharb commented 6 years ago

@john-aws for one, forward progress is always off limits if it would break the web - keeping websites working is far more important than any arbitrary syntax.