📜  JavaScript字符串fromCodePoint()

📅  最后修改于: 2020-09-27 07:05:00             🧑  作者: Mango

JavaScript String fromCodePoint()方法返回使用给定代码点序列创建的字符串 。

fromCodePoint()方法的语法为:

String.fromCodePoint(num1, ..., numN)

fromCodePoint()方法是静态方法,使用String类名称进行调用。


fromCodePoint()参数

fromCodePoint()方法采用:

  • num1,…,numN-代码点序列。

从fromCodePoint()返回值
  • 返回使用指定的代码点序列创建的字符串 。

注意事项

  • 如果给出了无效的Unicode代码点, fromCodePoint()方法将引发RangeError
  • fromCodePoint()方法返回一个字符串,而不是String对象。

示例:使用fromCodePoint()方法
let string1 = String.fromCodePoint(65, 66, 67);
console.log(string1); // ABC

let string2 = String.fromCharCode(72, 69, 76, 76, 79);
console.log(string2); // HELLO

// numbers can be passed as hexadecimals
let string3 = String.fromCodePoint(0x2f804);
console.log(string3); // "\uD87E\uDC04"

// unlike fromCharCode() that requires surrogate pair to return supplementary char
// fromCodePoint() can even return 4-byte supplementary chars
let string4 = String.fromCodePoint(0x1f303);
console.log(string4); // Unicode character "Night with Stars"

let string5 = String.fromCodePoint(Infinity);
console.log(string5); // RangeError

输出

ABC
HELLO
\uD87E\uDC04

RangeError: Invalid code point Infinity

推荐阅读: JavaScript字符串fromCharCode()