ES6语法入门——下

template string

$("#result").append(
  "There are " + basket.count + " " +
  "items in your basket, " +
  "" + basket.onSale +
  " are on sale!"
);//es5

$("#result").append(`
  There are ${basket.count} items
   in your basket, ${basket.onSale}
  are on sale!
`);//es6 用反引号(\)来标识起始,用${}`来引用变量,而且所有的空格和缩进都会被保留在输出之中

Destructuring(解构)

let cat = 'ken'
let dog = 'lili'
let zoo = {cat: cat, dog: dog}
console.log(zoo)  //Object {cat: "ken", dog: "lili"} es5

let cat = 'ken'
let dog = 'lili'
let zoo = {cat, dog}
console.log(zoo)  //Object {cat: "ken", dog: "lili"} es6
//反过来可以这么写:
let dog = {type: 'animal', many: 2}
let { type, many} = dog
console.log(type, many)   //animal 2

default(默认值), rest(数组参数)

function animal(type){
    type = type || 'cat'  
    console.log(type)
}
animal()//es5

function animal(type = 'cat'){
    console.log(type)
}
animal()//es6

//最后一个rest语法也很简单,直接看例子:
function animals(...types){
    console.log(types)
}
animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]

你可能感兴趣的:(jquery)