On the parameters of functions in JS

Source: Internet
Author: User

var a = 100;

function Test (a) {
a++; A (formal parameter) is a local variable
Console.log (a);
}
Test (a);
Console.log (a); The result is 101 100;

The parameters of the function are the internal variables of the function and cannot be accessed externally, even if they have the same name as the external variables; they are also two different variables

Similar to:
var a = 100;
function Test () {
var a=100; A is a local variable
a++;
Console.log (a);
}
Test ();
Console.log (a); Results 101 100;
*****************************************************

When a function does not have a parameter defined or there is no var variable internally, the internal variable defaults to a global variable; for example:

var a = 100;
function Test () {
a++;
Console.log (a);
}
Test ();
Console.log (a); The first one returns 101; the second 101;a is a global variable

******************************************************

When a function has a parameter defined and no argument is passed, the formal parameter defaults to undefined:

var a = 100;
function Test (a) {
a++;
Console.log (a);
}
Test ();
Console.log (a); The first one returns undefined; the second 100;

**************************************************************

However, if the parameter is passed through the object, the property value of the inner object of the function is changed, and the property value of the externally passed object will change;

var a= {x:100};
function Test (a) {
a.x++; Modifies the value of the Parameter object a.x, and a that is defined outside the function will also be modified
Console.log (a.x);
}
Test (a);
Console.log (a.x); The first one returns 101; the second 101;

Summary: When passing parameters by value
The arguments that are called in the function are function parameters (which belong to the category of local variables). If the function modifies the parameter value, the external referenced argument value will not be changed;
  When passing parameters through an object
An object property's variable is equivalent to a pointer, so when the function modifies the object's property value, the External object property value also changes
This is also a value that involves the base type and the reference type, and the base data type (Undefined,null,boolean,number) is accessed by value, so the local variable and the global variable even if two names one
and two different variables;
object is a reference data type, when the variable is an object, when the variable is assigned to another variable, the equivalent of another variable is also changed to a pointer; two
The volume points to the same object, so in the reference data type, the variable is equivalent to a pointer;
(Note: JavaScript Advanced programming fourth chapter variables, scope and memory are explained in detail);

On the parameters of functions in JS

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.