The difference between a value type and a reference type in JavaScript __java

Source: Internet
Author: User

The difference between a value type and a reference type in JavaScript

a , not stored the same way

L Basic Data type

A variable stores a simple data segment, storing a specific value, and a lightweight way of storing data.

L Reference type

A value of a reference type, an object that can consist of multiple values, and a variable of a reference type stores an object reference address. Reference type is the weight of the data stored way, allocated in heap memory, frequently creating objects damaging performance.

Reference type (n multiple) Object, Array, Date, Function 、......

Second, Dynamic properties are not the same

L reference types can dynamically add properties

var person=new Object ();

Person.name= "Jack";

person.age=20;

Console.log ("Name:%s, Age:%d", person.name,person.age);

Results


Description

Property Dynamic Add Success

L value type cannot be added

var person= "";

Person.name= "Jack";

person.age=20;

Console.log ("Name:%s, Age:%d", person.name,person.age);

Results


Description

Property has failed to add dynamically, indicating that this is a variable of the value type.

three , Different ways to assign values

L value type, content of copy variable

var num=10;

var num2=num;//creates a new space and copies num values to num2.

The contents of this two variable are num2, and the content of the contents is a copy of Num. duplicate Parts of a similar operating system

var num=10;

var num2=num;

num2=100;//does not affect the value of num

Console.log ("num=%d num2=%d", num,num2);

Results


Demo Memory Evolution (Field operations)

L reference type, copy reference address

var person1=new Object ();

person1.age=18;

var person2=person1;//copy reference address, give variable person2, then point to an object

person2.age=30;//is actually the object that Person1 pointed to.

Console.log ("person1.age=%d person2.age=%d", person1.age,person2.age);

Results


Description

All two variables point to the same object, the object is to be referenced by the variable, and the number of references is unrestricted.

Memory changes drawing (Field Operation)





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.