箭头功能的意外输出

时间:2017-01-11 05:26:37

标签: javascript arrow-functions

我有以下箭头功能。

var a = 10;
var b = 5;
var str = (a,b) => ("sum: "+(a+b));

str();

我预计输出为

sum: 15

但输出是

sum: NaN

为什么?

3 个答案:

答案 0 :(得分:2)

var a = 10;

var b = 5;

var str = (a, b) => ("sum: " + (a + b));

str(a, b); // You have to call the function with parameters. 

答案 1 :(得分:1)

您必须传递参数str(a,b)

var a = 10;
var b = 5;
var str = (a,b) => ("sum: "+(a+b));

str(a,b);

答案 2 :(得分:1)

您没有在调用函数

中传递参数ab



var a = 10;
var b = 5;
var str = (a, b) => ("sum: " + (a + b));
console.log(str(a, b));