Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

📝 [JavaScript] learn to form a record, [programmer essential knowledge]

📔 : Arguments are used in JavaScript functions

Writing in the front

In a function, our arguments are divided into parameters and arguments. When the function is called, we pass the arguments and when the function receives the arguments, we try to match the parameters with the arguments. But sometimes, we’re not sure how many arguments are passed in, how many arguments are passed in, so it’s hard to write parameters. Write less, the result is wrong, write more, is redundant!! 😢 what can we do at this time?

1. What are arguments

That provides a workaround in JavaScript where arguments can be used to resolve this situation!

Arguments is actually a built-in object for the current function in JavaScript. All functions have a built-in arguments object that stores all arguments passed through.

2. Arguments

 function funA() {
                 
       }
  funA(3.2.1)
Copy the code

We write a function called funA that passes three arguments but no parameters, simulating a situation where the user does not know how many arguments are passed to the function. What if you know how many arguments the caller passed in funA? We just mentioned arguments above, so use arguments to see! 😊

 // arguments
 function funA() {
    console.log(arguments) 
     }
  funA(3.2.1)
Copy the code

The print result is as follows

We print Arguments and you can see that Arguments contains the three values we passed, meaning Arguments stores all the Arguments passed in. Arguments are wrapped in brackets ([]). [] is an array literal. Only arrays are wrapped in brackets. Arguments is an array?

Actually show the arguments in the form of a pseudo array, therefore also has some characteristics of the array, also can undertake array traversal operations, but is not really a pseudo array of arrays, satisfy the following features is pseudo array.

Pseudo-arrays have the following characteristics:

  • withlengthattribute
  • Store data by index
  • Not having an arraypush/popEtc operation (note)

3. Give examples

Let’s print and see if we can output the length of arguments

function funA() {
  console.log('the arguments length: + arguments.length)\
   }
    funA(3.2.1)
Copy the code

Code execution can be printed out the length of the arguments, then index value if reflect, we can see that the argument on the diagram below

Since there are index features, then we can get the corresponding value according to the index value, let’s print and see!

// arguments
function funA() {

console.log(arguments)
console.log('the arguments length: + arguments.length)
console.log(arguments[2])

}
funA(3.2.1)
Copy the code

We can see that the value of index 2 is 1, so we can verify that the data is stored by index, so let’s go through the for loop

function funA() {

console.log(arguments)				
for (var i = 0; i <arguments.length; i++ ) {
		console.log(arguments[i])
	}
}
funA(3.2.1)
Copy the code

As can be verified from the print results, arguments can indeed use the traversal method as an array.

4. To summarize

  1. Only functions doarguments
  2. Each function has a built-in oneargumentDo not create it manually
  3. argumentswithlengthattribute
  4. argumentsStore data by index
  5. argumentsNot having an arraypush/popOperations such as

3. Write in the back

Follow me, more content continues to output

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹