简体   繁体   English

JavaScript参数,意外的数字错误

[英]JavaScript arguments, unexpected number error

When parsing the following argument the console.log states that the two numbers (arguments) are unexpected. 解析以下参数时, console.log指出两个数字(参数)是意外的。 Why is this and how could I fix this error? 为什么会这样,如何解决此错误?

function max(20, 20) {
 return width * height;
}
console.log(max(width, height));

It should be this way (your arguments cannot be direct values, it should be variables. And while calling them you should use values) 应该这样(您的参数不能是直接值,应该是变量。调用它们时,您应该使用值)

function max(width, height) {
 return width * height; 
}
console.log(max(20,20));

You don't have to pass values as arguments in definition of function, but when you'll call it, so your code should be like example bellow. 您不必在函数的定义中将值作为参数传递,但是在调用它时,因此您的代码应类似于下面的示例。

Hope this helps. 希望这可以帮助。


 //Definition function max(width , height) { return width * height; } //Call console.log(max(20, 20)); 

When you define a function you tell the function the arguments you are going to use ,it is something like defining a variable to use it inside the function.You can't define number as arguments.Try to do it like this: 当您定义一个函数时,您会告诉该函数要使用的参数,就像定义一个变量以在函数内部使用它一样。您不能将数字定义为参数。尝试这样做:

 function max(width , height) 
{
     return width * height;
}
console.log(max(20, 20));

and pass the numbers you want ,when you use the function ,not when you define it. 并在使用函数时传递所需的数字,而不是在定义函数时传递。

Note the changes: 注意更改:

function max(width, height) //pass 20 to both width and height by calling function this way->> max(20,20);.
       {
       return width * height; 
       } 
       console.log(max(width, height));  
      //printing width and height

Now,why is function max(20,20) wrong? 现在,为什么function max(20,20)错误?

  • Whatever goes inside a function paranthesis(argument) should be a variable declaration(or name of a predefined variable) ,it can not be a value. 函数paranthesis(argument)内部的任何内容都应为variable declaration(or name of a predefined variable) ,不能为值。 You had entered 20,20 and 20 is not treated as a variable because variable names cannot begin with a number in javascript and almost all other languages. 您输入了20,20并且不会将20视为变量,因为变量名不能以javascript和几乎所有其他语言的数字开头。
  • That is the reason you are getting an error. 这就是您遇到错误的原因。

Well,I know that you are confused because you have seen stuff like max(20,20); 好吧,我知道您很困惑,因为您已经看到过max(20,20);类的东西max(20,20); . Haven't you? 不是吗 This is called calling the function and passing value to it . 这称为calling the function and passing value to it I guess this is what you wanted to do. 我想这就是您想要做的。

  • max(20,20); is a statement in which you are calling the function max . 是在其中调用函数max的语句。 Here (20,20) is valid beacuse you can enter value as well because these values are supposed to be passed to width and height . 这里(20,20)是有效的,因为您也可以输入value,因为这些值应该传递给widthheight

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

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