簡體   English   中英

箭頭函數返回函數文本而不是結果(javascript)

[英]arrow function returns the function text and not the result (javascript)

箭頭函數返回函數文本-“(n)=> 5 + n”,而不返回結果(6)。 我究竟做錯了什么?

let n = 1;
let newText = (n) => 5 + n;
document.write(newText);

您沒有在調用該函數:

document.write(newText(42));

順便說let ,這與=>let無關:

function newText(n) { return 5 + n; }
document.write(newText);

有同樣的問題。

調用函數

document.write(newText(5));

您需要調用箭頭功能。

JS小提琴

let n = 1;
let newText = (n) => 5 + n;
document.write(newText(n));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM