at method for relative indexing

发布时间 · 标签: ECMAScript

The new at method on Array.prototype, the various TypedArray prototypes, and String.prototype makes accessing an element nearer to the end of the collection easier and more succinct.

Accessing the Nth element from the end of a collection is a common operation. However, the usual ways to do so are verbose, like my_array[my_array.length - N], or might not be performant, like my_array.slice(-N)[0]. The new at method makes this operation more ergonomic by interpreting negative indices to mean "from the end". The previous examples may be expressed as

For uniformity, positive indices are also supported, and are equivalent to ordinary property access.

This new method is small enough that its full semantics may be understood by this compliant polyfill implementation below:

function at(n) {
// Convert the argument to an integer
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += this.length;
// Out-of-bounds access returns undefined
if (n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];

A word about Strings #

Since at ultimately performs ordinary indexing, calling at on String values returns code units, just as ordinary indexing would. And like ordinary indexing on Strings, code units may not be what you want for Unicode strings! Please consider if String.prototype.codePointAt() is more appropriate for your use case.

at method support #

  • Chrome: 92 版本开始支持
  • Firefox: 90 版本开始支持
  • Safari: 不支持
  • Node.js: 不支持