An analysis of the implementation principle of the formal parameters and arguments of functions in JavaScript

Source: Internet
Author: User

We all know that JS inside the parameters of the transfer can be different, for example, we have a function:

 <  script  type  = "Text/javascript"     >  function   one (a,b,c) { this  .x  =   A;    Console.log (a); } One ( 1  ); </ script  >  

The number we passed was only one 1, but the formal parameter had ABC three. That's not right, is it?

This time if we look at B,c, it will show undefined. PS: for example Console.log (b).

Of course, there would have been no such two numbers. How can I get it for you?

In the face of such arguments and formal parameters of the case, JS is how to achieve it? Let's take a look at some code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metahttp-equiv= "X-ua-compatible"content= "Ie=edge,chrome=1"><title>The difference between the formal parameter and the actual parameter of a function in JavaScript</title></Head><Body>    <Scripttype= "Text/javascript">    functionOne (a,b,c) {returnone.length; }    functionBoth (a,b,c,d,e,f,g) {returnarguments.length; } console.log (one ()); //3Console.log (both ());//0    </Script></Body></HTML>

Here we return the one.length in the one function and return the arguments.length in the function . As you may have found, the output of one () returns one.length as the number of formal parameters, and argument.length is the number of arguments.

Seconds to understand the wood there?

We can even use arguments[0] to take the number of arguments.

But why do the numbers without formal parameters be represented in an array-like way? All right, I know you're a Virgo, and everything you need is right. You can use this unless you have an array of parameters. Otherwise in the function we can only use the name of the formal parameter to obtain the actual parameters.

In the C#,java of the type and number of relative JS requirements are very strict, a little on the error. And JS because of this design and can become a relatively flexible language.

The above method is the realization that JS relies on the equal number of formal parameters and actual parameters. As for the type of the same, JS what type can be written as var a = ..., what is the type? So it is said that JS is a weak type of language!

How to implement the parameters and arguments of functions in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.