为了账号安全,请及时绑定邮箱和手机立即绑定

这些箭头函数有什么区别

这些箭头函数有什么区别

互换的青春 2023-05-19 19:52:42
const post = text => (    .... )和const post = text => {     .... }我是新手,抱歉,如果这是一个愚蠢的问题,我已经搜索了一些文章,但没有得到它。有人可以帮忙解释一下吗
查看完整描述

2 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

const post = text => (

   ....

)

此箭头函数需要括号中的表达式或单个语句。调用该函数时将返回表达式的结果。不需要return明确地写。


例子:


const isOdd = num => ( num % 2 == 1 );

第二个箭头函数需要一个函数体。如果不明确返回,undefined将被退回。


const post = text => {

    ....

}

例子:


const isOdd = num =>{

   return num % 2 == 1;

}

在第一种形式中,你并不总是需要()around 表达式,但当你返回一个对象文字时它是必需的。


const Point = (x,y) => {x,y};

console.log(Point(0,0)); //undefined

const Point = (x,y) => ({x,y});

console.log(Point(0,0)); //{ x: 1, y: 0 }


查看完整回答
反对 回复 2023-05-19
?
HUWWW

TA贡献1874条经验 获得超12个赞

第一个箭头函数表示返回一个值,(what is return) 第二个箭头函数表示您想要定义的函数{define your function} 以获取更多描述,请遵循此示例:


const post = text => (text) // return text


const post2 = text =>{ //define your function

  return (text)

}


console.log(post("hello post"));

console.log(post("hello post2"));


查看完整回答
反对 回复 2023-05-19
  • 2 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信