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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 时尚主编晓雪在她的《优雅》一书中说过这样的话,“最不会穿的女人,就是把一身logo穿在身上,再漂亮也是人家logo...
    豪小阅读 1,111评论 0 2
  • 以前,对于《人性的弱点》这类书,可能会敬而远之。道理都懂,长篇大论,比较枯燥。 当身处环境不同时,你会发现这是一本...
    芦荟加冰阅读 3,362评论 1 1
  • (2)地铁五号线 自从我知道小米死后,心里老是觉得过意不去,心神不宁。 一个星期过后,小米的所有行李都从我家收拾干...
    释清风阅读 2,870评论 0 1