参数无效的函数表达式

时间:2018-01-19 20:42:56

标签: javascript

我正在编写带参数的函数表达式,它看起来像这个

var n1 = 10;
var n2 = 15;

var num = function (n1, n2) {return (n1 > n2) ? n1 : n2;}

console.log("The larger number of " + n1 + " and " + n2 + " is " + num);

但是在控制台中我收到的消息是

The larger number of 10 and 15 is function (n1, n2) {return (n1 > n2) ? n1 : n2;}

我无法弄清楚我在这里做错了什么......

3 个答案:

答案 0 :(得分:1)

console.log("The larger number of " + n1 + " and " + n2 + " is " + num);更改为console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));

var n1 = 10;
var n2 = 15;

var num = function (n1, n2) {return (n1 > n2) ? n1 : n2;}

console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));

答案 1 :(得分:1)

使用num(n1, n2)代替num,因为您必须调用该函数。提示:尝试使用AirBnb Javascript style guide

console.log(`The larger number of ${n1} and ${n2} is ${num(n1,n2)}`);

答案 2 :(得分:1)

输入参数并执行函数:

console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));