RangeError: argument is not a valid code point

當將 NaN 值、負整數 (-1)、非整數 (5.4) 或大於 0x10FFFF (1114111) 的值與 String.fromCodePoint() 一起使用時,會發生 JavaScript 異常“無效程式碼點”。

訊息

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 (1114111) 的值時,會丟擲此錯誤。

程式碼點是 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𝌇'

另見