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

你可能感兴趣的:(JS笔记:ES6 Syntactical Sugar)