【问题标题】:Why am I getting ".at" is not a function?为什么我得到“.at”不是一个函数?
【发布时间】:2021-07-21 04:46:51
【问题描述】:

我知道如何使用[] 对数组进行索引,但我正在尝试使用Array.at() 方法根据其索引显示数组中的项目,如此处所述MDN Docs - Array.at

但我收到以下错误:

未捕获的类型错误:arr1.at 不是函数

我仔细检查过,一切正常,但我不知道出了什么问题。

这是我的代码:

const arr1 = [10, 20, 30, 40, 50];

const res = arr1.at(2);
console.log(res);

注意:这与建议的 How to get value at a specific index of array In JavaScript? 副本不同。这个问题是关于访问数组的方法,这个问题是为什么一个新的 API 不可用以及如何纠正它。

【问题讨论】:

  • @decpk 首先你为什么直接提出这个问题?请不要那样做,使初学者失去动力。此外它的存在你可以在这里查看“developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • @safinghoghabori 显然 .at() 在您的环境中不存在。您正在遇到错误。 .at() 非常新 - 它仍然是 a proposal(第 3 阶段),甚至没有添加到 the latest draft of the ES specs。因此,支持它的环境非常很少。您可以在页面底部看到表格。
  • At 更适合访问最后一个值 - 即将推出 - tc39.es/ecma262/#sec-array.prototype.at
  • @CertainPerformance,请重新审核。这个问题不是所列问题的欺骗
  • @SafinGhoghabori 是的,您正在引用文档。但是您是否也检查了浏览器兼容性表?例如 Chrome >= 92 或 nodeJs >= 16.6。您的环境是否满足此要求?可能不是……

标签: javascript arrays


【解决方案1】:

如果您收到此消息,则表明您在其上运行代码的任何平台都不支持该方法。这是相当新的 - 虽然大多数浏览器的最新版本 support it,但 2021 年之前的任何东西肯定不会。这个方法是only very recently 于(2021 年 8 月结束)签署的,并被纳入官方规范,因此它不会存在于旧环境中。要么升级你的环境,要么添加一个 polyfill。

根据提案文档,在大多数情况下应该符合标准的“粗略 polyfill”是:

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 });
}

在尝试使用 .at 之前只需运行它,您应该能够使用它,即使在较旧的不兼容环境中也是如此。如果您愿意,也可以安装 this more exhaustive shim

【讨论】:

  • 它在我的家用电脑上工作,但在工作电脑上却没有,我一直在摸不着头脑,直到我读到这篇文章。更新了节点,现在它可以工作了:)
【解决方案2】:

代码在 Stackoverflow 代码终端中运行良好。由于 JS 版本控制,您的机器可能不支持相同的。该方法是最近介绍的。

【讨论】:

    【解决方案3】:

    一种选择是使用core-js 库...

    JavaScript 的模块化标准库。包括 polyfills 直到 2021 年的 ECMAScript:承诺、符号、集合、迭代器、 类型化数组,许多其他特性,ECMAScript 提案,一些 跨平台的 WHATWG / W3C 功能和提案,例如 URL。你可以 仅加载所需的功能或在没有全局命名空间的情况下使用它 污染。

    先用 npm 或 yarn 安装:

    npm install core-js
    

    yarn add core-js
    

    然后像这样在你的 JS 或 TS 项目中使用它:

    import 'core-js/features/array/at';
    
    let arr = [];
    arr.push(42);
    console.log(arr.at(0));
    

    请注意,上面的代码只导入了数组的 at 方法。导入所有 polyfill 或子集:

    // polyfill all `core-js` features, including early-stage proposals:
    import "core-js";
    // or:
    import "core-js/features";
    // polyfill all actual features - stable ES, web standards and stage 3 ES proposals:
    import "core-js/actual";
    // polyfill only stable features - ES and web standards:
    import "core-js/stable";
    // polyfill only stable ES features:
    import "core-js/es";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2022-01-02
      • 2021-12-13
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多