RangeError: argument is not a valid code point

JavaScript 异常“Invalid code point”会在 String.fromCodePoint() 方法与 NaN 值、负整数(-1)、非整数(5.4)或大于 0x10FFFF(1114111)的值一起使用时抛出。

错误信息

RangeError: Invalid code point -1 (V8-based)
RangeError: -1 is not a valid code point (Firefox)
RangeError: Arguments contain a value that is out of range of code points (Safari)

错误类型

什么地方出错了?

String.fromCodePoint() 方法在被传入 NaN 值、负整数(-1)、非整数(5.4)或大于 0x10FFFF 的值时抛出该错误。

码位(code point)是 Unicode 代码空间中的数值,即范围为 00x10FFFF 的整数。

示例

无效的示例

js
String.fromCodePoint("_"); // RangeError
String.fromCodePoint(Infinity); // RangeError
String.fromCodePoint(-1); // RangeError
String.fromCodePoint(3.14); // RangeError
String.fromCodePoint(3e-2); // RangeError
String.fromCodePoint(NaN); // RangeError

有效的示例

js
String.fromCodePoint(42); // "*"
String.fromCodePoint(65, 90); // "AZ"
String.fromCodePoint(0x404); // 'Є' (U+0404)
String.fromCodePoint(0x2f804); // '你' (U+2F804)
String.fromCodePoint(194564); // '你'
String.fromCodePoint(0x1d306, 0x61, 0x1d307); // '𝌆a𝌇'

参见