送你一个大西瓜通过本文主要向大家介绍了ES6等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
$("#result").append(
"There are <b>" + basket.count + "</b> " +
"items in your basket, " +
"<em>" + basket.onSale +
"</em> are on sale!"
);//es5
$("#result").append(`
There are <b>${basket.count}</b> items
in your basket, <em>${basket.onSale}</em>
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"]