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"
//遍歷碼點(diǎn)
let text = String.fromCodePoint(0x20BB7);
for (let i = 0; i < text.length; i++) {
console.log(text[i]);//ES5中大于0x20BB7不能識(shí)別
}
// " "
// " "
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!"
最后編輯于 :2019.03.14 19:32:16
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者