3 ms·
Subtle readability improvements on patterns that see extensive use actually make a difference. I had the exact same thoughts when `.includes()` was proposed to
by johnfn 5y ago
Subtle readability improvements on patterns that see extensive use actually make a difference.
I had the exact same thoughts when `.includes()` was proposed to be added to the spec - it's just `.indexOf() !== -1`! - but time has proven me wrong, and I suspect it will prove us wrong about `at` as well.
I've written an `.indexOf() !== -1` check hundreds if not thousands of times, and I've gotten the conditional wrong enough times to actually need to revert a change in prod.
I have similar thoughts about .at(). Taking an element from the end of the array is ever-so-slightly error prone. Sure, you won't make a mistake this time, or the next time, but write it a hundred times, or a thousand, and I bet a bug will slip in. It's darn near impossible to get `.at(-1)` wrong, however.
- beaconstudios 5y agoThat's actually a pretty reasonable argument, the 'length - N' form is actually quite prone to off-by-one errors so .at might help there.
- NicolaiS 5y agoIndeed, and as others have stated it is really hard to allow `[-1]` without breaking old code. Also: `[1, NaN, "hi"].indexOf(NaN)` is -1 but `.includes(NaN)` returns true as expected.
- nawgz 5y ago> `[1, NaN, "hi"].indexOf(NaN)` is -1 but `.includes(NaN)` returns true Brutal.
- alerighi 5y agoExcept you would never get to use this method, because it will take years for all browser vendors to implement it, and even then, you still have a number of users that use for a reason or another Internet Explorer. You need to use a transpiler (like Typescript), but at this point, there no reason to implement that in the language, a transpiler can implement the feature by simply adding the method to the prototype of the object, like this: Array.prototype.at = function (i) { return i < 0 ? this[this.length + i] : this[i] }