ES6学习笔记--default,rest

default 意思是默认值。大家可以看下面的例子,调用animal()方法时忘记了传参数,传统的做法就是加上这一句type= type || 'cat' 来指定默认值。

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

而ES6S则可以直接给形参添加默认值,如:

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

rest

function animals(...types){
    console.log(types);
}
animals('cat','dog','fish') //["cat","dog","fish"]

如果不用ES6的话,则要使用ES5的arguments

posted @ 2017-05-27 17:34  沐浴点阳光  阅读(259)  评论(0编辑  收藏  举报