简体   繁体   English

箭头函数返回函数文本而不是结果(javascript)

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

The arrow function returns the function text - "(n) => 5 + n" and not the result (6). 箭头函数返回函数文本-“(n)=> 5 + n”,而不返回结果(6)。 What am I doing wrong? 我究竟做错了什么?

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

You're not calling the function: 您没有在调用该函数:

document.write(newText(42));

This has nothing to do with => or let , by the way: 顺便说let ,这与=>let无关:

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

has the same problem. 有同样的问题。

Call the function 调用函数

document.write(newText(5)); document.write(newText(5));

You need to call the arrow function. 您需要调用箭头功能。

JS Fiddle 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