In-depth understanding of JavaScript function parameters

Source: Internet
Author: User

Arguments

JavaScript has a very arbitrary parameter requirement, and she doesn't care what data type you pass in. The parameter is not even passed. In fact, JavaScript function calls do not even check the number of incoming parameters.

1 function Add (x) {2         returnX +1;3     }4Console.log (Add (1));//25Console.log (Add ('1'));// One6Console.log (Add ());//NAN7Console.log (Add (1,2,5));//2

Formal parameter with the same name

In non-strict mode, a function can have the same name parameter, but the eng of the last occurrence ...

    function Add (x,x,x,) {        return  x;    }    Console.log (Add (2,5,6//6

Number of parameters

CASE1: Fewer arguments than parameters? The rest of the formal parameters are set to undefined

1     function Add (x, y) {2        console.log (x, y)3    }4     Add (1//  1,undefined

At this point we can set a reasonable default value for Y

1     function Add (x, y) {2         100003        console.log (x, y); 4     }5     Add (1//1 10000

CASE2: More formal parameter than actual parameter? More is a waste of! can be obtained by arguments[i].

The parameter is then internally represented by an array, and the arguments object is not an instance of the array, it is an array of classes object.

1 function Add () {2Console.log (arguments[0],arguments[1],arguments[2]);// the3Console.log (arguments[0]+arguments[1]);//34Console.log (arguments.length);//45     }6Add1,2,3,4);

CASE3: As many formal parameters as arguments? The named parameter is the same as the value of the corresponding arguments object, but not the same namespace. In other words, the two values are synchronized and the namespace is independent.

Callee

The arguments object has a callee attribute, which is a pointer to the function that owns the arguments object, as shown in factorial

   function factorial (num) {        if1) {            return1;        }  Else{            return  1);        }    }    Factorial (6);  720
1 function factorial (num) {2         if(Num <=1){3             return 14}Else{5             returnnum * Arguments.callee (num-1);6         }7     }8Factorial (6);//720

Caller

The caller property of the function holds the application of the function that invokes the current function, and if the current function is called in global use, its value is null

This one doesn't mean anything, it's fun.

1     Function mother () {2        Son (); 3     }4    function son () {5        mother (); 6     }7     mother ();   // uncaught rangeerror:maximum Call stack size exceeded 8     // Stack Overflow ... 

This is the right path.

1     Function mother () {2        Son ()3    }4     function Son () {5        console.log (son.caller); 6     }7     mother ();  // function mother () {son ()}

The caller of the arguments object is always undefined, so that it is defined in order to separate from the caller of the function.

1     function mother (x) {2        console.log (arguments.caller); 3     }4     mother (+//undefined

Parameter passing

CASE1: Base Type value

 1  function addten (num) { 2  num + = 10  ;  3  return   num;  4  }  5  var  count = 20   6  var  result =  Addten (count);  7  Console.log (count); // 20, no change  8  console.log (result); // 30  

CASE2: Reference type value

When the value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable will be reflected inside the function. )

1     function SetInfo (obj) {2         'lihong'3    }  4     varnew  Object (); 5     setInfo (person); 6     // Lihong

When you override a reference type's formal parameter inside a function, the variable refers to a local object. This local object is destroyed immediately after the function is executed.

    function SetInfo (obj) {        'lihong';        Console.log (person.name);         New Object ();         ' Linyao ' ;        Console.log (Person.name);    }     var New Object ();

Operation Result: "Husband" "husband" explain what? Wives don't matter! Ha ha

In-depth understanding of JavaScript function parameters

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.