Unicode表示法\u{}
"\uD842\uDFB7"
// "𠮷"
"\u{20BB7}"
// "𠮷"
"\u{41}\u{42}\u{43}"
// "ABC"
let hello = 123;
hell\u{6F} // 123
'\z' === 'z' // true
'\172' === 'z' // true
'\x7A' === 'z' // true
'\u007A' === 'z' // true
'\u{7A}' === 'z' // true
codePointAt()
var s = "𠮷";
s.length // 2
s.charAt(0) // ''
s.charAt(1) // ''
s.charCodeAt(0) // 55362
s.charCodeAt(1) // 57271
let s = '𠮷a';
s.codePointAt(0) // 134071
s.codePointAt(1) // 57271
s.codePointAt(2) // 97
let s = '𠮷a';
s.codePointAt(0).toString(16) // "20bb7"
s.codePointAt(2).toString(16) // "61"
let s = '𠮷a';
for (let ch of s) {
console.log(ch.codePointAt(0).toString(16));
}
// 20bb7
// 61
function is32Bit(c) {
return c.codePointAt(0) > 0xFFFF;
}
is32Bit("𠮷") // true
is32Bit("a") // false
String.formCodePoint()
//ES5
String.fromCharCode(0x20BB7)
// "ஷ"
//ES6
String.fromCodePoint(0x20BB7)
// "𠮷"
String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'
// true
字符串变遍历器
for (let codePoint of 'foo') {
console.log(codePoint)
}
// "f"
// "o"
// "o"
//遍历码点
let text = String.fromCodePoint(0x20BB7);
for (let i = 0; i < text.length; i++) {
console.log(text[i]);//ES5中大于0x20BB7不能识别
}
// " "
// " "
for (let i of text) {
console.log(i);
}
// "𠮷"
String.raw()
String.raw`Hi\\n`
//"Hi\\n"
String.raw`Hi\n${2+3}!`;
//"Hi\n5!"
String.raw`Hi\u000A!`;
//"Hi\u000A!"