简体   繁体   English

在javascript函数中传递参数不起作用

[英]Passing arguments in a javascript function is not working

I am bit unclear in understanding why this below piece of code is not showing the message box, 我不太清楚为什么下面的这段代码没有显示消息框,

http://jsfiddle.net/KendoDev/p8Mk2/ http://jsfiddle.net/KendoDev/p8Mk2/

 function findMax(var x, var y) {

//alert("x is:" + x + " y is: " +y);
    var max = 0;
    if(x>y)
         max = x;
    else
         max = y;
    alert("max is: " + max);
    return max;
} 
var c=4,d=9;
var m = findMax(c,d);

您的函数语法错误:在参数列表中不需要var

function findMax(x, y) {

Your javascript isn't syntactically correct - function findMax(var x, var y) should just be function findMax(x, y) . 您的JavaScript语法上不正确- function findMax(var x, var y)应该只是function findMax(x, y)

In general, if you write some javascript and nothing happens when you run it, it probably means something went wrong syntactically, and you should check your browser's Error Console. 通常,如果您编写了一些javascript脚本,但是在运行它时却没有任何反应 ,则可能意味着语法上出现了问题,您应该检查浏览器的错误控制台。 (In this case, it tells you that the function definition is where the error occurred, right on the first "var".) (在这种情况下,它告诉您函数定义是发生错误的位置,就在第一个“ var”上。)

Remove var from arguments. 从参数中删除var。 Like this: 像这样:

function findMax(x, y) {

//alert("x is:" + x + " y is: " +y);
    var max = 0;
    if(x>y)
         max = x;
    else
         max = y;
    alert("max is: " + max);
    return max;
} 
var c=4,d=9;
var m = findMax(c,d);

you do not need to put a var 你不需要放一个var

see updated fiddle 看到更新的小提琴

http://jsfiddle.net/p8Mk2/2/ http://jsfiddle.net/p8Mk2/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM