Live data from Hacker News

Stage 3 Proposal: Array.prototype.at

tc39.es

11–20 of 153 posts

Re: Stage 3 Proposal: Array.prototype.at

#11

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.

Re: Stage 3 Proposal: Array.prototype.at

#12
post #8
post #3

Earlier quoted context omitted.

A quick read suggests that it adds negative indexing from the end of the array.

Imho that violates the principle of least astonishment - that "at" would have such different behavior from "[]" for negative numbers.

That's the whole point of this though... Currently you can't use negative indexes, but this would make them usable.

Re: Stage 3 Proposal: Array.prototype.at

#13
post #4
post #2

Am I reading this correctly — it's an alias for arr[i] and str[i]? I know languages like Ruby have arr.at(i), but why?

Accessing negative indexes currently returns undefined. This adds support for indexing from the end of an array with negative numbers.

This works, but not the way you'd think:

    const arr = [];
    arr[-1] = 'Hello world';
    console.log(arr[-1]);
    // Hello world
Instead of using the negative index, it stringifies the "-1" and uses it as the key of an object both when writing and reading. Thus, arr.length still remains 0

Re: Stage 3 Proposal: Array.prototype.at

#14

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.

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.

Re: Stage 3 Proposal: Array.prototype.at

#15
post #7
post #2

Am I reading this correctly — it's an alias for arr[i] and str[i]? I know languages like Ruby have arr.at(i), but why?

In addition to negative index support, a function is more easily composable e.g. converting a list of indices to a list of objects: `indices.map(myObjectArr.at)` I've lost count on how many times I've wrote this function manually.

> `indices.map(myObjectArr.at)`

That works in this case, but eta-reduction is not generally safe in Javascript due to the variadic nature of many functions, so I wouldn't recommend it in production code. For example `indices.forEach(console.log)` does not work as one would expect.

Re: Stage 3 Proposal: Array.prototype.at

#17

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.

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++.

Re: Stage 3 Proposal: Array.prototype.at

#18
post #7

Earlier quoted context omitted.

In addition to negative index support, a function is more easily composable e.g. converting a list of indices to a list of objects: `indices.map(myObjectArr.at)` I've lost count on how many times I've wrote this function manually.

> `indices.map(myObjectArr.at)` That works in this case, but eta-reduction is not generally safe in Javascript due to the variadic nature of many functions, so I wouldn't recommend it in production code. For example `indices.forEach(console.log)` does not work as one would expect.

Better a function that works in some cases than not having a function at all. Also, by using TypeScript definitions you pretty much always know what's coming in and going out and VS Code will yell at you if you're trying to do something stupid.

Re: Stage 3 Proposal: Array.prototype.at

#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.

Re: Stage 3 Proposal: Array.prototype.at

#20

This is going to confuse C++ developers where vector::at() explicitly throws exceptions on out of bounds access But I guess the intersect between C++/JS developers is too small to care

As one of those c++/js developers I'd just say that I do not map concepts from one language to another because it rarely works anyways.
Post reply on HN