亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這些箭頭函數有什么區別

這些箭頭函數有什么區別

互換的青春 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 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號