1 无参数
let hehefunc = ()=>{
//some codes...
console.log("hehe");
}
image.png
2 一个参数,可以省略括号
let hehefunc2 = name=>{
//some codes
console.log("name is: "+name);
}
image.png
3 2个及以上参数,且定义了默认参数
let hehefunc3 =(name="bob",age=18)=>{
//some codes
console.log("name is: "+name+"age is:"+age);
}
image.png
4 只有单行返回语句时,可以省略花括号,省略return,如下:
let hehefunc4 =(name="bob",age=18)=>{
//some codes
return "name is "+name+" age is "+age;
}
let hehefunc5 =(name="bob",age=18)=>
"name is "+name+" age is "+age;
image.png
5 返回对象情况下,因为箭头不能指向一个对象的花括号(被解析为函数体),我们可以用括号将对象括起来
let hehefunc6 =(name="bob",age=18)=>
({"name":name,"age":age,"job":"developer"})
image.png
6 如果函数体有多行语句,那么与普通函数一样,没写return默认返回undefined,有写return就返回return内容
网友评论