Live data from Hacker News

Stage 3 Proposal: Array.prototype.at

tc39.es

21–30 of 153 posts

Re: Stage 3 Proposal: Array.prototype.at

#21
post #19

If I understand this correctly it is to add negative index support to arrays. How will findIndex work when this is introduced? It currently uses -1 as a return value when no element satisfies the testing function.

You can’t have a negative array index. Passing a negative number will just make this function count backwards instead of forwards.

[1, 2, 3, 4].at(0) === 1

[1, 2, 3, 4].at(1) === 2

[1, 2, 3, 4].at(-1) === 4

findIndex’s behavior is unchanged.

Re: Stage 3 Proposal: Array.prototype.at

#22
post #19

If I understand this correctly it is to add negative index support to arrays. How will findIndex work when this is introduced? It currently uses -1 as a return value when no element satisfies the testing function.

It’s not adding negative indexes, a negative number will index from the end.

So [0, 1, 2, 3].at(-2) === 2

Re: Stage 3 Proposal: Array.prototype.at

#23

So instead of fixing the language and allow to use array[-1], they add an other way to access array element. This is why I don't like JavaScript, instead of fixing feature, they add new feature to fix previous feature.

There is existing code out there which relies on negative indexes to return undefined. Or relies on being able to assign items to negative indexes and then retrieve them, this is perfectly valid code: `a=[];a[-1]=42;console.log('the answer is',a[-1])`. The web tries really hard to be backwards compatible and not break existing code.

Maybe we should re utilize the "use strict"; type of metadata. Something like "use es2021" to enable negative indexes etc.

Re: Stage 3 Proposal: Array.prototype.at

#24

So instead of fixing the language and allow to use array[-1], they add an other way to access array element. This is why I don't like JavaScript, instead of fixing feature, they add new feature to fix previous feature.

There is existing code out there which relies on negative indexes to return undefined. Or relies on being able to assign items to negative indexes and then retrieve them, this is perfectly valid code: `a=[];a[-1]=42;console.log('the answer is',a[-1])`. The web tries really hard to be backwards compatible and not break existing code.

How about deprecating that for a few years then? Doesn't seem good to keep the behavior, given that it will also be confusing in the future.

But perhaps we just don't know enough, and they will add the `at`, and at some point actually do bind `arr[index]` to use the implementation of that function?

Re: Stage 3 Proposal: Array.prototype.at

#25
post #19

If I understand this correctly it is to add negative index support to arrays. How will findIndex work when this is introduced? It currently uses -1 as a return value when no element satisfies the testing function.

Python also uses negative indexes in this way, see https://docs.python.org/3/library/stdtypes.html#common-seque...

Re: Stage 3 Proposal: Array.prototype.at

#27

Earlier quoted context omitted.

New features are added to JavaScript very carefully to avoid breaking existing code on the web. The downside is you often end up with multiple ways to do the same thing, but there are ways to mitigate this, like using a linter to enforce using a modern subset of the language.

That's starting to sound an awful lot more like C++.

[deleted]

Re: Stage 3 Proposal: Array.prototype.at

#28
post #24

Earlier quoted context omitted.

There is existing code out there which relies on negative indexes to return undefined. Or relies on being able to assign items to negative indexes and then retrieve them, this is perfectly valid code: `a=[];a[-1]=42;console.log('the answer is',a[-1])`. The web tries really hard to be backwards compatible and not break existing code.

How about deprecating that for a few years then? Doesn't seem good to keep the behavior, given that it will also be confusing in the future. But perhaps we just don't know enough, and they will add the `at`, and at some point actually do bind `arr[index]` to use the implementation of that function?

How would you deprecate it? There's tons of browsers and JS runtimes out there and they all adopt features at different speeds. Undoubtedly some browsers would never adopt the new feature, which means websites will simply break, and the web will become even more fragmented than it already is.

Re: Stage 3 Proposal: Array.prototype.at

#29
post #24

Earlier quoted context omitted.

There is existing code out there which relies on negative indexes to return undefined. Or relies on being able to assign items to negative indexes and then retrieve them, this is perfectly valid code: `a=[];a[-1]=42;console.log('the answer is',a[-1])`. The web tries really hard to be backwards compatible and not break existing code.

How about deprecating that for a few years then? Doesn't seem good to keep the behavior, given that it will also be confusing in the future. But perhaps we just don't know enough, and they will add the `at`, and at some point actually do bind `arr[index]` to use the implementation of that function?

There's a large body of existing code out there, some of which might rely on the current behavior but never be updated. I doubt any length of deprecation period would solve this issue.

Instead, adding .at() allows having the new feature now, and in a way that's possible to polyfill for backwards compatibility.

Re: Stage 3 Proposal: Array.prototype.at

#30
post #19

If I understand this correctly it is to add negative index support to arrays. How will findIndex work when this is introduced? It currently uses -1 as a return value when no element satisfies the testing function.

Array.prototype.at would only ever return the value of something in the array at the index requested (or undefined). It'd accept a negative offset, but that's not the index of the thing you're looking for; it's an offset from one end of the array.
Post reply on HN