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.
Stage 3 Proposal: Array.prototype.at
11–20 of 153 posts
Re: Stage 3 Proposal: Array.prototype.at
#12Earlier 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.
Re: Stage 3 Proposal: Array.prototype.at
#13Am 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.
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 0Re: Stage 3 Proposal: Array.prototype.at
#14So 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.
Re: Stage 3 Proposal: Array.prototype.at
#15Am 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.
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
#16But I guess the intersect between C++/JS developers is too small to care
Re: Stage 3 Proposal: Array.prototype.at
#17So 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
#18Earlier 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.
Re: Stage 3 Proposal: Array.prototype.at
#19Re: Stage 3 Proposal: Array.prototype.at
#20This 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