W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
6.1?字符串使用單引號?''
?。
// bad
const name = "Capt. Janeway";
// good
const name = 'Capt. Janeway';
6.2?字符串超過 80 個字節(jié)應(yīng)該使用字符串連接號換行。
6.3?注:過度使用字串連接符號可能會對性能造成影響。jsPerf?和?討論.
// bad
const errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
// bad
const errorMessage = 'This is a super long error that was thrown because \
of Batman. When you stop to think about how Batman had anything to do \
with this, you would get nowhere \
fast.';
// good
const errorMessage = 'This is a super long error that was thrown because ' +
'of Batman. When you stop to think about how Batman had anything to do ' +
'with this, you would get nowhere fast.';
6.4?程序化生成字符串時,使用模板字符串代替字符串連接。
為什么?模板字符串更為簡潔,更具可讀性。
// bad
function sayHi(name) {
return 'How are you, ' + name + '?';
}
// bad
function sayHi(name) {
return ['How are you, ', name, '?'].join();
}
// good
function sayHi(name) {
return `How are you, ${name}?`;
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: