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 }

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"));
添加回答
舉報