For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/year/pending/string-prototype-at.md.
  • 简体中文
  • String.prototype.at ?

    提案概览
    提案速览

    该提案定义了 String.prototype.at 的 polyfill,用于提取字符串中指定位置的码点,越界时返回空字符串。它包含逐步算法,处理代理对。该提案未被纳入规范,后来另一个语义不同的提案被采用。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    注意: 此提案未被上游纳入 ECMAScript 规范!本仓库仅为历史目的保留。多年后,在 2020 年 11 月,一个不同的提案 添加了具有不同语义的 String.prototype.at

    ES6/ES7 String.prototype.at polyfill 构建状态

    一个健壮且优化的、兼容 ES3 的 polyfill,用于 ECMAScript 6/7 的 String.prototype.at 提案

    规范错误票证:https://bugs.ecmascript.org/show_bug.cgi?id=2073

    String.prototype.at(pos) 的规范提案

    注意: 返回一个单元素字符串,其中包含将 this 对象转换为字符串后得到的字符串 value 中位置 pos 处的码点。如果该位置没有元素,则结果为空字符串。结果是字符串值,而非字符串对象。

    当调用 at 方法并传入一个参数 pos 时,执行以下步骤:

    1. ORequireObjectCoercible(this value)
    2. SToString(O)
    3. ReturnIfAbrupt(S)
    4. positionToInteger(pos)
    5. ReturnIfAbrupt(position)
    6. sizeS 中的元素个数。
    7. 如果 position < 0position ≥ size,返回空字符串。
    8. first 为字符串 S 中索引 position 处的码元。
    9. cuFirst 为字符串 first 中索引 0 处的码元值。
    10. 如果 cuFirst < 0xD800cuFirst > 0xDBFFposition + 1 = size,则返回 first
    11. cuSecond 为字符串 S 中索引 position + 1 处的码元值。
    12. 如果 cuSecond < 0xDC00cuSecond > 0xDFFF,则返回 first
    13. second 为字符串 S 中索引 position + 1 处的码元。
    14. cp(first – 0xD800) × 0x400 + (second – 0xDC00) + 0x10000
    15. 返回 cp 的 UTF-16 编码(第 6 条)的元素。

    注意: at 函数有意设计为通用的;它不要求其 this 值必须是字符串对象。因此,它可以被转移到其他类型的对象上作为方法使用。

    安装

    在浏览器中:

    <script src="at.js"></script>

    通过 npm

    npm install string.prototype.at

    然后,在 Node.js 中:

    require('string.prototype.at');
    
    // 在 Windows 和默认设置的 Mac 系统上,大小写不敏感,
    // 因此你可以这样代替:
    require('String.prototype.at');

    备注

    String.fromCodePointString.prototype.codePointAt 的 polyfill 和测试套件也可用。

    作者

    许可证

    此 polyfill 根据 MIT 许可证提供。