JS筆記:ES6 Syntactical Sugar

Templating

Basically you can use the same syntax as groovy string interpolation.

// Multiline strings
`This is a legal
string in ES 6.`

// String interpolation
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`

Variable Arguments

Vanilla JS arguments array

function foo() {
  console.log(arguments);
}

foo(1, 2, 3); // [1, 2, 3]

ES6 Rest Parameters

function foo(a, b, ...theArgs) {
  // ...
}
  • rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function;
  • the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach
    pop can be applied on it directly;

ES6 Default Parameters

function foo(x=12) {
  console.log(x);
}

foo(); // 12
foo(1); // 1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 時(shí)尚主編曉雪在她的《優(yōu)雅》一書(shū)中說(shuō)過(guò)這樣的話,“最不會(huì)穿的女人,就是把一身logo穿在身上,再漂亮也是人家logo...
    豪小閱讀 162評(píng)論 0 2
  • 以前,對(duì)于《人性的弱點(diǎn)》這類(lèi)書(shū),可能會(huì)敬而遠(yuǎn)之。道理都懂,長(zhǎng)篇大論,比較枯燥。 當(dāng)身處環(huán)境不同時(shí),你會(huì)發(fā)現(xiàn)這是一本...
    蘆薈加冰閱讀 496評(píng)論 1 1
  • (2)地鐵五號(hào)線 自從我知道小米死后,心里老是覺(jué)得過(guò)意不去,心神不寧。 一個(gè)星期過(guò)后,小米的所有行李都從我家收拾干...
    釋清風(fēng)閱讀 373評(píng)論 0 1