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。
新聞熱點
疑難解答