6.5 码点的数字值

新方法 codePointAt() 返回字符串中指定位置字符的数字值:

const str = 'x\uD83D\uDE80y';
console.log(str.codePointAt(0).toString(16)); // 78
console.log(str.codePointAt(1).toString(16)); // 1f680
console.log(str.codePointAt(3).toString(16)); // 79

该方法在字符串遍历中也能正确执行:

for (const ch of 'x\uD83D\uDE80y') {
    console.log(ch.codePointAt(0).toString(16));
}
// Output:
// 78
// 1f680
// 79

codePointAt() 对应的方法是 String.fromCodePoint()

> String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'
true

results matching ""

    No results matching ""