JavaScript variables, values and addresses, relationships between parameters

Source: Internet
Author: User

Let's dry the harvest first:

The 1.javascrip variable contains two types of values, one for the reference type and one for the base type. Reference types include: Array,object,function (as you can understand, non-basic types are reference types); 5 basic types include: Undefined,null,string,boolean,number

2. The mechanism for passing the parameters of a function is to copy the value of the variable.

The book says, "Copy the values from the outside of the function to the parameters inside the function, and copy the value from one variable to another." The delivery of a primitive type is like a copy of a primitive type variable, whereas a reference type is like a copy of a reference type variable. “

"When a variable copies a value of a reference type, it also copies the value stored in the variable object into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer, and this pointer points to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. So changing one of the variables will affect the other. “

"Note: The value of the copy reference type is the address"

3. The parameter is actually a local variable of the function.

----------------------------------------------------------------------------

Explanation of the basic concept:

Transfer Value : The value of A to B, change B,a will not follow, B is the same as a value;
Address: transfer A to B, change b,a at the same time, b only the address of a (like a computer shortcut).

A data with a value type is stored in a variable within the stack. That is, allocate memory space in the stack, store the value directly, and its value represents the data itself. Data of a value type has a faster access speed.

A data with a reference type (reference type) does not reside in the stack, but is stored in the heap. That is, allocating memory space in the heap, not directly storing the contained value, but pointing to the value to be stored, whose value represents the address pointed to. When accessing a data with a reference type, you need to check the contents of the variable into the stack, which refers to an actual data in the heap. Data of a reference type data ratio type has greater storage size and lower access speed.

----------------------------------------------------------------------------

Here are three questions.

Question 1: Why is the outside a not disturbed after the change (a) function is executed?

<Script>vara= [1, 2, 3];functionChange (a) {Console.log (a);//[A]a= 2; //Pass ValueConsole.log (a);//2}change (a); Console.log (a); //[A]</Script>

Question 1: Since the execution of Change (a) is like this, the object A (array) is first passed into the make, and is copied to the change parameter a. The a=2 is an assignment statement that becomes the value of the pass. At this point, a=2 is a value type and does not involve an issue referring to addresses. So it doesn't affect the outside of a.

Question 2: Why is the outside a disturbed after the change (a) function is executed?

<Script> vara= [1, 2, 3]; functionChange () {a= 2;//Pass Value} change ();   Console.log (a); //2</Script>

Question 2: When performing a change (), the function finds the scope chain in its own execution environment, the active object (activation object) does not contain the variable A, then goes up the scope chain, finds the global execution environment, discovers the variable A, So the inside of the function of a and external a in memory is the same address, the natural function inside a has changed, the outside will also change.

Parsing: The difference between Issue 2 and issue 1 is that problem 2 does not introduce parameters, so it does not involve copying variables.

Question 3: Why is the outside a disturbed after the change (a) function is executed?

<Script> vara= [1, 2, 3]; functionChange (b) {b[0] = 2; }  Change (a);   Console.log (a); //[2,2,3]</Script>

Question 3 Answer: This and the problem 1 very similar, but is not the same is a=2, replaced with b[0]=2, I was very confused at first, do not say copy it? The parameter B should be a copy value, how does it affect the outside a?

Indeed, when the change function executes, parameter b is the copy value of a. Because A is a reference type, inside the function are B and a by reference to an object that is an address. The appearance of b[0]=2 does not affect the same object that is referenced by B and a within the function.

Question 4: Why is the outside a not disturbed after the change (a) function has been executed?

var a = [1, 2, 3];  function Change (b) {     console.log (b);//[1,2,3]    b=2;    B[0] = 2; } Change  (a);  Console.log (a);   [A]

Question 4 Answer: Change (b) The execution process is like this, the A object passes in the function, and copies the value and address to B. b=2 This sentence, when B becomes a value type, does not involve the problem of address reference, then b[0]=2 this sentence actually meaningless, because at this time B is not an array, nature does not have b[0] such an index way. So the address reference relationship between B and a actually disappears after the b=2. At this time the outside world A is still [a];

Reference books:

JavaScript Advanced Programming (3rd edition)

js-Reference and Replication (value and address) HT tp://blog.csdn.net/zzzaquarius/article/details/4902235

JavaScript variables, values and addresses, relationships between parameters

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.