×

JS报错:RangeError: argument is not a valid code point

作者:Terry2020.04.16来源:Web前端之家浏览:6400评论:0

在JS里执行项目的时候出现JS报错:RangeError: argument is not a valid code point。我们一起来分析下原因:

错误信息

RangeError: {0} is not a valid code point (Firefox)
RangeError: Invalid code point {0} (Chrome)

什么地方出错了?

 String.fromCodePoint() 这个方法只能接受有效的码位(code point) 。

码位( code point)是组成码空间(或代码页)的数值,范围是 0 到 0x10FFFF。

NaN,负整数(-1),非整数(3.14),或编号大于0x10FFFF (1114111) 的字符,无法使用该方法。

范例

无效的例子

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

有效的例子

String.fromCodePoint(42);       // "*"
String.fromCodePoint(65, 90);   // "AZ"
String.fromCodePoint(0x404);    // "\u0404"
String.fromCodePoint(0x2F804);  // "\uD87E\uDC04"
String.fromCodePoint(194564);   // "\uD87E\uDC04"
String.fromCodePoint(0x1D306, 0x61, 0x1D307) // "\uD834\uDF06a\uD834\uDF07"

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/jsbaocuo001.html

网友评论文明上网理性发言 已有0人参与

发表评论: