首页 > 其他 > 详细

ES6学习笔记--default,rest

时间:2017-05-27 18:28:18      阅读:201      评论:0      收藏:0      [点我收藏+]

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

ES6学习笔记--default,rest

原文:http://www.cnblogs.com/garfieldzhong/p/6913854.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!