Skip to content

RangeError: 无效 code 点

错误信息

                无效 code 点 U+XXXX
              

快速摘要

你 called 字符串.fromCodePoint() 使用值 那个是 不有效 Unicode code 点 — 它必须是 非-负 integer 上到 0x10FFFF.

为什么发生

  • 通过负 数字到 字符串.fromCodePoint()

  • 通过数字 greater than 0x10FFFF (最大值 Unicode code 点)

  • 通过非-integer or NaN

最小示例

✗ 错误代码
const char = String.fromCodePoint(-1);
✓ 修复代码
const code = Math.max(0, Math.min(0x10FFFF, Math.floor(-1)));
const char = String.fromCodePoint(code);

负 code 点是 无效. Clamp 值到 有效范围 [0, 0x10FFFF].

如何诊断

  • Log 值是 通过到 字符串.fromCodePoint()

  • 检查那个它 是非-负 integer 在内有效 Unicode 范围

  • 验证输入之前 using 它

如何修复

  • 验证 code 点是 之间 0 and 0x10FFFF

  • Use Math.floor() 到 ensure integer

  • Provide fallback for 无效 code 点

如何预防

  • 验证 Unicode code 点之前 passing 它们到 字符串.fromCodePoint()

相关资源

相关术语

相关课程

← 返回语言