代码人»首页 技术广场 Vue ECMAScript 6 查看内容

ES6-箭头函数

329

主题

3

回帖

1388

积分

管理员

积分
1388
箭头函数的进本组成包括:函数参数,箭头,函数体。
1.语法
①.一个参数
  1. let count = pic => pic;
  2. //相当于:
  3. function count(pic){
  4.   retun pic;
  5. }
复制代码

②.多个参数
  1. let count = (pic,num) => `${pic},{num}`;
  2. //相当于
  3. function count(pic,num){
  4.   retun pic + "," + num;
  5. }

复制代码
③.空参数,需要一个空小括号()
  1. let car = () =>"明细描述!"
复制代码

④.函数体多个语句,需要用函数体包含起来

  1. let count = (pic,num)=>{
  2. let total = pic*num;
  3. return total;
  4. }

  5. console.log(count(2,5))//10

复制代码

⑤.箭头返回是一个对象

  1. let count = (pic,num) =>({
  2. pic:pic,
  3. num:num
  4. })
  5. console.log(count(2,5))//{pic:2,num:5}
复制代码



上一篇: ES6-对象

下一篇: 没有了

举报 回复