此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
RangeError: argument is not a valid code point
JavaScript 异常“Invalid code point”会在String.fromCodePoint() 方法与NaN 值、负整数(-1)、非整数(5.4)或大于 0x10FFFF(1114111)的值一起使用时抛出。
In this article
错误信息
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)
错误类型
RangeError什么地方出错了?
String.fromCodePoint() 方法在被传入NaN 值、负整数(-1)、非整数(5.4)或大于 0x10FFFF 的值时抛出该错误。
码位(code point)是 Unicode 代码空间中的数值,即范围为0 到0x10FFFF 的整数。
示例
>无效的示例
js
String.fromCodePoint("_"); // RangeErrorString.fromCodePoint(Infinity); // RangeErrorString.fromCodePoint(-1); // RangeErrorString.fromCodePoint(3.14); // RangeErrorString.fromCodePoint(3e-2); // RangeErrorString.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𝌇'