2016-06-25 135 views
-1

箭头函数返回函数文本 - “(n)=> 5 + n”而不是结果(6)。我究竟做错了什么?箭头函数返回函数文本而不是结果(javascript)

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

你不调用该函数..不喜欢'文件撰写(newText(N));' – Redu

+0

另外请注意,N'隐藏变量'N'内的参数'箭头功能。如果箭头函数需要访问变量'n',请为该参数使用不同的名称。 –

+0

阅读JavaScript教程以更好地理解函数可能会使您受益:http://eloquentjavascript.net/03_functions.html。 –

回答

5

你不调用该函数:

document.write(newText(42)); 

这有什么好做=>let,顺便说一句:

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

有同样的问题。

+0

为什么这不起作用?让n = 1; document.write((n)=> 123 + n); –

+0

@AlexeyTseitlin工作正常:它将函数写入文档。 – melpomene

+0

但它会执行它... –

0

调用函数

document.write(newText(5));

0

您需要调用箭头功能。

JS Fiddle

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

为什么这不起作用?让n = 1; document.write((n)=> 123 + n); –

+0

这不是箭头函数的正确行为。 – Manish