我们在Javascript中将函数的参数(变量或其值)称为什么?

时间:2021-12-15 16:58:01

OK lets take an example:

好吧举个例子:

function student(name,age){
document.write("I am " + name + "and I am " + age + " years old." );
}
var student_name = "Divyansh";
var student_age = 17;
student(student_name,student_age);

// now my question is what do we call the arguments of the function student :

//现在我的问题是我们称之为函数学生的参数:

(a) is it the variable: student_name & student_age respectively,

(a)它是变量:student_name&student_age,

(b) or is it the value contained by the variables: divyansh & 17 respectively

(b)或者是变量所包含的值:divyansh和17

PS- I have also posted the same question on codechef, but it seems nobody is interested in giving answer to my stupid and childish question. But, what do i do i am trying to study Javascript myself by the book head first javascript programming and got strucked due to this doubt in one of the question, Please Help.

PS-我也在codechef上发布了相同的问题,但似乎没有人有兴趣回答我的愚蠢和幼稚的问题。但是,我该怎么办我试图通过本书头部第一次javascript编程来研究Javascript,并且由于这个疑问中的一个问题,请帮助。

5 个解决方案

#1


If I understood your question correctly, you want to know whether you should say

如果我理解你的问题,你想知道你是否应该说

I called student with student_name and student_age.

我用student_name和student_age打电话给学生。

or

I called student with "Divyansh" and 17.

我打电话给学生“Divyansh”和17岁。

right?

Well, that depends on the whole context, and sometimes it's possible to use both.
And this is to some extent a question of opinion, but I'll try to justify my opinion as good as I can.

嗯,这取决于整个背景,有时可以同时使用两者。这在某种程度上是一个意见问题,但我会尽力证明我的意见。

I believe you should refer to the arguments by their names (i.e. student_name and student_age if

我相信你应该通过他们的名字来引用这些论点(即student_name和student_age if

  • their value is of no significance for what you're saying.
    Unnecessary information usually leads to confusion.
  • 他们的价值对你说的话毫无意义。不必要的信息通常会导致混淆。

  • their value is unknown.
  • 他们的价值未知。

  • their value is long and you previously mentioned it.
    Long values such as Math.pow((5*x+7*y-35*v*x*z)/(42*m/35*k+47*f+99*l), 35*22*88*k) tend to make written text messy and are really annoying to speak out many times in a row, so you'll want to refer to them by name.
  • 他们的价值很长,你之前提到过它。长数值如Math.pow((5 * x + 7 * y-35 * v * x * z)/(42 * m / 35 * k + 47 * f + 99 * l),35 * 22 * 88 * k)倾向于使书面文字变得杂乱,并且连续多次说出来真的很烦人,所以你想要通过名字来引用它们。

You should refer to the arguments by their value when their value matters, e.g. when you have a bug that only occurs when certain values are passed to your function, but not some others.

当它们的值很重要时,你应该通过它们的值来引用参数,例如:当你有一个只在某些值传递给你的函数时才会发生的错误,而不是其他一些错误。

Now there is a gap between those, where I believe you can use both.
For that case, I'd apply the golden rule
As short as possible, but as long as required.

现在这些之间存在差距,我相信你可以同时使用这两者。对于这种情况,我会尽可能地应用黄金法则,但只要需要。

If you had the following code:

如果您有以下代码:

var name = "Divyansh";
console.log(name);
var name = "Marc";
console.log(name);
var name = "Jon Skeet";
console.log(name);

Then I believe all of the following statements are perfectly valid:

然后我相信以下所有陈述都是完全有效的:

I called console.log three times with name as argument.

我以控制名称为参数调用了console.log三次。

or

I called console.log three times with "Divyansh", "Marc" and "Jon Skeet" as argument.

我用“Divyansh”,“Marc”和“Jon Skeet”作为参数,三次调用console.log。

But it really depends on how much information you need or want to give.

但这实际上取决于您需要或想要提供多少信息。

#2


An argument of a function is an expression with which the function is called.

函数的参数是用于调用函数的表达式。

In this case, the function is called with two expressions as arguments: student_name and student_age. These expressions yield a value when evaluated: the value of the variables.

在这种情况下,使用两个表达式作为参数调用函数:student_name和student_age。这些表达式在计算时会产生一个值:变量的值。

#3


The function is called with two parameters. These parameters are the variables. And the function get the values of those variables and assign it in its local variable namely 'name' and 'age'.

使用两个参数调用该函数。这些参数是变量。函数获取这些变量的值并将其分配在其局部变量中,即“name”和“age”。

#4


Have you used console.log() method? Have you ever wondered what's the maximum number of parameters it takes? Answer is as many as it can.

你使用过console.log()方法吗?你有没有想过它需要的最大参数数量是多少?答案是尽可能多的。

Lets take an example.

让我们举个例子。

function print(x,y){
  console.log(x);
  console.log(y);
}

Well this is an easy function, takes 2 arguments and print them in the console.

这是一个简单的功能,需要2个参数并在控制台中打印它们。

Lets say I have 5 parameters passed to function print. a,b,c,d,e. How would you modify the print method? May be something like this. (Don't write programs like this. It ain't effective).

可以说我有5个参数传递给函数print。 A,B,C,d,E。你会如何修改打印方法?可能是这样的。 (不要写这样的程序。它没有效果)。

function print(a,b,c,d,e){
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
  console.log(e);
}

How about if I pass 100 parameters like, a1, a2, ... a100. What if I pass 1000 parameters. You can't write 1000 console.log statements to print them. So you would think of a for loop to print all the parameters.

如果我传递100个参数,如a1,a2,... a100,怎么样?如果我传递1000个参数怎么办?您无法编写1000个console.log语句来打印它们。所以你会想到一个for循环来打印所有参数。

But wait!

How do I access all the parameters inside a for loop unless if they are an array (use for-loop or for-in to iterate) or an object (use for-in to iterate)?

我如何访问for循环中的所有参数,除非它们是一个数组(使用for循环或for-in迭代)或一个对象(使用for-in迭代)?

Now comes the object "arguments". This is a built-in object implicitly created by javascript for every function you declare.

现在出现了对象“论点”。这是javascript为您声明的每个函数隐式创建的内置对象。

What does this store? It stores all the parameters you passed to the function in the form of object.

这个商店是什么?它以object的形式存储您传递给函数的所有参数。

arguments { 0: a, 1: b, 2: c, 3: d, 4: e };

参数{0:a,1:b,2:c,3:d,4:e};

Now I have something like an object that holds all the parameters passed to the function, it will be easier to access them in a for-in loop. Now my print method will look like:

现在我有一个像对象一样的东西,它包含传递给函数的所有参数,在for-in循环中访问它们会更容易。现在我的打印方法将如下所示:

function print(a,b,c,d,e,f) {
  for (var prop in arguments) {
    console.log(arguments[prop]);
  }
}

This will loop through all the properties of the arguments object and prints the value stored in each property.

这将循环遍历arguments对象的所有属性,并打印存储在每个属性中的值。

What is stored in each property of argument object?

什么存储在参数对象的每个属性中?

fn parameter object-property value stored in property

a                  0             value of parameter a
b                  1             value of parameter b
c                  2             value of parameter c
d                  3             value of parameter d
e                  4             value of parameter e
f                  5             value of parameter f

#5


In your case you are passing two arguments named student_name and student_age to student function which contains value Divyansh and 17 respectively.

在您的情况下,您将两个名为student_name和student_age的参数传递给学生函数,该函数分别包含值Divyansh和17。

#1


If I understood your question correctly, you want to know whether you should say

如果我理解你的问题,你想知道你是否应该说

I called student with student_name and student_age.

我用student_name和student_age打电话给学生。

or

I called student with "Divyansh" and 17.

我打电话给学生“Divyansh”和17岁。

right?

Well, that depends on the whole context, and sometimes it's possible to use both.
And this is to some extent a question of opinion, but I'll try to justify my opinion as good as I can.

嗯,这取决于整个背景,有时可以同时使用两者。这在某种程度上是一个意见问题,但我会尽力证明我的意见。

I believe you should refer to the arguments by their names (i.e. student_name and student_age if

我相信你应该通过他们的名字来引用这些论点(即student_name和student_age if

  • their value is of no significance for what you're saying.
    Unnecessary information usually leads to confusion.
  • 他们的价值对你说的话毫无意义。不必要的信息通常会导致混淆。

  • their value is unknown.
  • 他们的价值未知。

  • their value is long and you previously mentioned it.
    Long values such as Math.pow((5*x+7*y-35*v*x*z)/(42*m/35*k+47*f+99*l), 35*22*88*k) tend to make written text messy and are really annoying to speak out many times in a row, so you'll want to refer to them by name.
  • 他们的价值很长,你之前提到过它。长数值如Math.pow((5 * x + 7 * y-35 * v * x * z)/(42 * m / 35 * k + 47 * f + 99 * l),35 * 22 * 88 * k)倾向于使书面文字变得杂乱,并且连续多次说出来真的很烦人,所以你想要通过名字来引用它们。

You should refer to the arguments by their value when their value matters, e.g. when you have a bug that only occurs when certain values are passed to your function, but not some others.

当它们的值很重要时,你应该通过它们的值来引用参数,例如:当你有一个只在某些值传递给你的函数时才会发生的错误,而不是其他一些错误。

Now there is a gap between those, where I believe you can use both.
For that case, I'd apply the golden rule
As short as possible, but as long as required.

现在这些之间存在差距,我相信你可以同时使用这两者。对于这种情况,我会尽可能地应用黄金法则,但只要需要。

If you had the following code:

如果您有以下代码:

var name = "Divyansh";
console.log(name);
var name = "Marc";
console.log(name);
var name = "Jon Skeet";
console.log(name);

Then I believe all of the following statements are perfectly valid:

然后我相信以下所有陈述都是完全有效的:

I called console.log three times with name as argument.

我以控制名称为参数调用了console.log三次。

or

I called console.log three times with "Divyansh", "Marc" and "Jon Skeet" as argument.

我用“Divyansh”,“Marc”和“Jon Skeet”作为参数,三次调用console.log。

But it really depends on how much information you need or want to give.

但这实际上取决于您需要或想要提供多少信息。

#2


An argument of a function is an expression with which the function is called.

函数的参数是用于调用函数的表达式。

In this case, the function is called with two expressions as arguments: student_name and student_age. These expressions yield a value when evaluated: the value of the variables.

在这种情况下,使用两个表达式作为参数调用函数:student_name和student_age。这些表达式在计算时会产生一个值:变量的值。

#3


The function is called with two parameters. These parameters are the variables. And the function get the values of those variables and assign it in its local variable namely 'name' and 'age'.

使用两个参数调用该函数。这些参数是变量。函数获取这些变量的值并将其分配在其局部变量中,即“name”和“age”。

#4


Have you used console.log() method? Have you ever wondered what's the maximum number of parameters it takes? Answer is as many as it can.

你使用过console.log()方法吗?你有没有想过它需要的最大参数数量是多少?答案是尽可能多的。

Lets take an example.

让我们举个例子。

function print(x,y){
  console.log(x);
  console.log(y);
}

Well this is an easy function, takes 2 arguments and print them in the console.

这是一个简单的功能,需要2个参数并在控制台中打印它们。

Lets say I have 5 parameters passed to function print. a,b,c,d,e. How would you modify the print method? May be something like this. (Don't write programs like this. It ain't effective).

可以说我有5个参数传递给函数print。 A,B,C,d,E。你会如何修改打印方法?可能是这样的。 (不要写这样的程序。它没有效果)。

function print(a,b,c,d,e){
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
  console.log(e);
}

How about if I pass 100 parameters like, a1, a2, ... a100. What if I pass 1000 parameters. You can't write 1000 console.log statements to print them. So you would think of a for loop to print all the parameters.

如果我传递100个参数,如a1,a2,... a100,怎么样?如果我传递1000个参数怎么办?您无法编写1000个console.log语句来打印它们。所以你会想到一个for循环来打印所有参数。

But wait!

How do I access all the parameters inside a for loop unless if they are an array (use for-loop or for-in to iterate) or an object (use for-in to iterate)?

我如何访问for循环中的所有参数,除非它们是一个数组(使用for循环或for-in迭代)或一个对象(使用for-in迭代)?

Now comes the object "arguments". This is a built-in object implicitly created by javascript for every function you declare.

现在出现了对象“论点”。这是javascript为您声明的每个函数隐式创建的内置对象。

What does this store? It stores all the parameters you passed to the function in the form of object.

这个商店是什么?它以object的形式存储您传递给函数的所有参数。

arguments { 0: a, 1: b, 2: c, 3: d, 4: e };

参数{0:a,1:b,2:c,3:d,4:e};

Now I have something like an object that holds all the parameters passed to the function, it will be easier to access them in a for-in loop. Now my print method will look like:

现在我有一个像对象一样的东西,它包含传递给函数的所有参数,在for-in循环中访问它们会更容易。现在我的打印方法将如下所示:

function print(a,b,c,d,e,f) {
  for (var prop in arguments) {
    console.log(arguments[prop]);
  }
}

This will loop through all the properties of the arguments object and prints the value stored in each property.

这将循环遍历arguments对象的所有属性,并打印存储在每个属性中的值。

What is stored in each property of argument object?

什么存储在参数对象的每个属性中?

fn parameter object-property value stored in property

a                  0             value of parameter a
b                  1             value of parameter b
c                  2             value of parameter c
d                  3             value of parameter d
e                  4             value of parameter e
f                  5             value of parameter f

#5


In your case you are passing two arguments named student_name and student_age to student function which contains value Divyansh and 17 respectively.

在您的情况下,您将两个名为student_name和student_age的参数传递给学生函数,该函数分别包含值Divyansh和17。