Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How many parameters can be passed when calling a function

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how many parameters can be passed when calling a function". In daily operation, I believe that many people have doubts about how many parameters can be passed when calling a function. The editor consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful to answer the question of "how many parameters can be passed when calling a function"! Next, please follow the editor to study!

You can pass in some parameters or variables when you create a function and call it, but how many parameters are available for a called function?

Available variables in a function

The following is an example to check which variables and parameters will be used when the function is called, where you can run this code directly in the browser's "scratch window" (which prevents the browser plug-in from affecting the operation).

Note: this is only for traditional functions, the running variables of arrow functions are different from traditional functions, and will not be discussed here.

Var globalVariable = 'global variable'; var obj = {aFunction: function (para) {var localVariables = 'local variable'; console.log (para, localVariables, arguments, this, globalVariable); / / contains the passed parameter}} obj.aFunction ('I am a description', 2,3)

Switch to the Source page at run time and verify it using Chrome's JavaScript debug mode.

Next, when you enter the function of aFunction, you can switch to console.log (...). Once the line stops, the result is as follows:

Next, you will see that Scope represents the scope of the current function, and the callable variables in the scope are listed in order. The variables and parameters you can see here include:

Para: the parameter passed in when it is called.

Arguments: the external parameter that the traditional function takes by default, which comes from the parameter passed externally. You can take the value here even if the function itself is not passed in. More details will be given later in this article.

This: the variables passed in when the function is running, and the way the function is called will affect its direction. I will write another article to introduce it later.

LocalVariables: a local variable that can only be called within the scope of this function.

Global: global variable

Of the above five variables, the first two belong to the variables passed in during the call, and this article will also focus on their characteristics.

Properties of parameters

Parameters are variables passed in by the tone function. Compared with other programming languages, JS has fewer restrictions on passing parameters, and any value can be used as a parameter. Because of this, there are many unfamiliar points for those who first come into contact with or transferred from other languages. Here are some common problems:

1. The parameter name is defined when the function is declared

This is a very basic problem. Beginners who are new to programming often confuse parameter names and mistakenly think that the parameter name passed in during the call is the name used in the function, such as what is the output value of the following example?

A. 'axiajiaoyuanjiaoyuanjiaoyuanjiaoyuanzhongyuanzhongyuanzhong undefined

B. 'dawning, writing, recording, writing, reading, etc.

Function callMore (d, c, b, a) {console.log (d, c, b, a);} var a = 'a'; var b =' b'; var c = 'cations; callMore (a, b, c)

The name of the function parameter is determined when the function is defined, as shown in the following figure: the value of the parameter is brought in sequentially and is not affected by the name at the time of the call.

The simple conclusion is:

The parameter name is not affected by the call name

If you encounter a parameter that is declared but does not pass in a value, it is undefined

If it is not defined, but more parameters are passed in, you need to take the value in another way.

2. Default values of parameters for ES6

If the parameter has been defined but has not been passed in, then the value taken in the function is undefined. In this case, the code must add a lot of fault-tolerant mechanisms to avoid errors created by undefined.

Parameter default values have been added to ES6 to set default values for specific parameters in advance, which not only avoids errors caused by undefined, but also increases the flexibility of using functions.

The following example adds a "parameter default value" through a simple syntax, which is used when there is no external input value.

Function getMoney (money = 1000) {console.log (`I have ${money} `);} getMoney (); / / No parameters are required

3. Arguments

What if I cannot confirm the number of parameters passed in?

This situation is relatively rare in project development, but it is very common in the development of frameworks and function libraries. In the previously mentioned case of "if it is not defined, but more parameters are passed in, then you need to use other ways to take values", you will use the arguments parameters that will be described below (there is a better method in ES6).

Function callMore (d, c, b, a) {/ / Note: the defined d, c, b, a parameters console.log (arguments) are not used here;} var a = 'a'; var b =' b'; var c = 'cations; callMore (a, b, c)

All traditional functions have arguments parameters (note: the arrow function does not), can be called directly without another definition, and the scope is limited to this function.

The structure of arguments is essentially an array that contains all the values passed in at the time of the call, which is a useful method when you are uncertain about the number of parameters passed in.

4. ES6 residual parameters

Because arguments has some shortcomings, such as:

The structure is similar to an array, but most array methods cannot be used directly

Overlaps with defined parameter content and cannot be used only for additional passed-in parameters

Therefore, ES6 adds the remaining parameter syntax, which can be passed in the remaining undetermined parameters directly when defining the parameters. The syntax is as follows:

Const callMore = (a,... args) = > {console.log (args);} var a = 'a'; var b =' b'; var c = 'cations; callMore (a, b, c)

The result is similar to the previous example, but it will be presented as a pure array, and only undefined parameter contents will be obtained (a will be skipped). In addition, the arrow function can use the remaining parameters.

5. Function can also be used as a parameter

In addition to passing in pure values, arrays and objects, functions can also take functions as parameters, making the function runtime richer and-- er. No, no, no. Complicated.

In the following example, when you call functionB, you can also pass in another function as an argument, which is called a callback function function.

Function functionB (fn) {fn ('Xiao Ming');} functionB (function (name) {console.log (name + 'Hello');}); at this point, the study of "how many parameters can be passed when calling a function" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report