Organize JavaScript base data and reference data copy value issues

Source: Internet
Author: User

JavaScript data is divided into two main categories: 1. Underlying type (raw type data) 2. Reference type. The way they're stored is different.The underlying type of data store is stored in the stack memory:

For example: Var a=1; var b=a;

1 var a=1; 2 var b=A; 3 a=2; 4 alert (b); // 1

The above code first assigns a value of 1 to the variable A, there is a stack in memory such as, and then declare the variable B, and the value of the variable a copy to the variable B, in the stack memory of the variable A, a value of 1, and then change the value of the variable A is 2, but they are independent in the stack memory, so it The value of the last popup B is 1.

data storage methods for reference types:

For example: Var a=new Object (); A.name= "Yewenxiang";

1 var a=New  Object (); 2       A.name="Yewenxiang"; 3 var b=A; 4       A.name="Xiangwang"; 5 alert (b.name); // Xiangwang

Objects in a reference type are stored in different ways than the underlying data type, and the object is in heap memory, and the stack memory stores the variable name and the address of the object in the heap memory.

The first line: declares that variable a creates an object instance that is stored in heap memory.

The second line: Adds a Name property to the A object with a value of "Yewenxiang", saved in heap memory,

The third line: Declare a variable B, in the stack memory of a point to the object's address is copied to the variable B, two addresses point to the same object.

Row four: Change the property value of name in the object to "Xiangwang"

Line five: Why the pop-up is "Xiangwang", because they point to the same object, and the uplink code to the name of the property value "Xiangwang", so the value of B.name is also "Xiangwang".

the difficulties encountered:
1 var a={name: "Yewenxiang"}; 2 var b=A; 3 a={name: "Xiangwang"}; 4 alert (b.name); // "Yewenxiang"

Why pop Yewenxiang, this question I just started thinking wrong: Start thinking they are all pointing to the same object, changed the value of A.name to "Xiangwang", so the value of B.name will also change to "Xiangwang". The problem occurs in the third line of code, A={name:xiangwang}, which does not change the value of the Name property to "Xiangwang" in the first line of the object, but instead re-creates another object with a Name property that has a value of " Xiangwang ", if you do not want to create a new object should write the third line of code a.name=" Xiangwang ", the Last value that pops up is" Xiangwang ".

Organize JavaScript base data and reference data copy value issues

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.