Why am I getting “.at” is not a function?

If you get this message, whatever platform you’re running the code on does not support the method yet. It’s quite new – while the most recent versions of most browsers support it, anything before 2021 definitely won’t. This method was only very recently signed off on (end of August 2021) and incorporated into the official specification, so it won’t exist in older environments. Either upgrade your environment, or add a polyfill.

Per the proposal document, a “rough polyfill” that should be standards-compliant for most cases is:

function at(n) {
    // ToInteger() abstract op
    n = Math.trunc(n) || 0;
    // Allow negative indexing from the end
    if (n < 0) n += this.length;
    // OOB access is guaranteed to return undefined
    if (n < 0 || n >= this.length) return undefined;
    // Otherwise, this is just normal property access
    return this[n];
}

const TypedArray = Reflect.getPrototypeOf(Int8Array);
for (const C of [Array, String, TypedArray]) {
    Object.defineProperty(C.prototype, "at",
                          { value: at,
                            writable: true,
                            enumerable: false,
                            configurable: true });
}

Simply run that before trying to use .at, and you should be able to use it, even on older incompatible environments. You can also install this more exhaustive shim instead if you wish.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)