美文网首页
JS笔记:ES6 Syntactical Sugar

JS笔记:ES6 Syntactical Sugar

作者: 开水的杯子 | 来源:发表于2017-08-05 12:05 被阅读8次

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

相关文章

网友评论

      本文标题:JS笔记:ES6 Syntactical Sugar

      本文链接:https://www.haomeiwen.com/subject/rbvilxtx.html