default很简单,意思就是默认值。大家可以看下面的例子,调用animal()方法时忘了传参数,传统的做法就是加上这一句type = type || ‘cat’来指定默认值。
function animal(type){
type = type || 'cat'
console.log(type)
}
animal()
如果用ES6我们而已直接这么写:
function animal(type = 'cat'){
console.log(type)
}
animal()
最后一个rest语法也很简单,直接看例子:
function animals(...types){
console.log(types)
}
animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]
而如果不用ES6的话,我们则得使用ES5的arguments。