Variable re-assignment and reference re-assignment problems in JavaScript

Source: Internet
Author: User

This is a common problem in javascript:

 var A=3;var b=a;a=5; The result was 5;; The result is 3;  and the situation in the following code has changed:  var A=1,b=2,c=3;var array=[a,b,c];a=5;; The result of   //was 1, and there was no change;   var A=1,b=2,c=3;var Array=[a,b,c]; array[0]=5;; The result is that 1,a has not changed   in the following code the situation is different:  var A=1,b=2,c=3;var array=[a,b,c]; Array2=array; array[0]=5;; The result is 5;  var A=1,b=2,c=3;var Array=[a,b,c];var Array2=array; array2[0]=5;; The result is that 5;  next explains the different reasons for the above code execution: first, the stack and heap problems, the stack is stored in the basic types of variables and object references, their values are stored directly in the stack, and the heap is stored in a complex data types, such as array objects and object objects, Their reference variables are stored in the stack, pointing to the actual objects stored in the heap.   The reason for this is that the data in the stack can be shared, in the first code, a=3 execution of the stack for the value of 3 allocated space, while B=a, b=3, the key is when a value changed to 5 after the value of B has not changed, this is because the data in the stack can be shared. If the execution of a=3,b=3;a=3 execution of 3 allocated memory, then b=3 will not be allocated in the stack memory storage 3 This value, but let B to point to the existing 3, when a=5, the program to find the stack there is no 5 this value, if there is a to point to 5, if not, then reallocate memory storage 5 , shown in the example above, a=5 re-allocated memory, a points to 5, and B points to a value of 3, and does not change because of the value of a.   uses arrays in the second program, arrays are stored in the heap, when an array is created, an array object is created in the heap, and a reference to the arrays is created in the stack, pointing to the actual objects stored in the heap. So when Array=[a,b,c] executes, array[0]=1,array[1]=2,array[2]=3; When the value of a changes to a=5 execution, a checks the stack for a value of 5, if there is a direct point to 5, if not, allocate memory storage 5,a point to 5 , but the value of array[0] did not change; Arrry[0]When =5 executes, the data in the heap is actually changed, without affecting the value of a in the stack. ARRAY=[A,B,C] is equivalent to copying the value of a,b,c into the heap.   In the third program, Array2=array, when the value of Array changes, the object in the heap is actually changed, so a value changes, and the corresponding value of the two array changes

Variable re-assignment and reference re-assignment problem 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.