Values for JavaScript primitives and reference types

Source: Internet
Author: User

The JS variable may contain values for two different data types, a base type value and a reference type value. Basic types refer to simple data segments, whereas reference type values are objects that consist of multiple values;

When assigning a value to a variable, the parser must determine whether the value is a base type value or a reference type value. JS has 5 basic data types: Undefined,

Null,boolean,number and String. These 5 basic data types are accessed by value. Because you can manipulate the actual values that are saved in the variable.

The value of the reference type is stored in the object in memory. Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly. When an object is manipulated, it is actually a reference to an object, not an actual object, and for that reason the value of the reference type is accessed by reference. Reference is one of the basic concepts of JS object-oriented. It is a pointer to the actual location of the object. The actual object is definitely not a reference. For example, a string is always a string, but multiple variables can point to the same object.
Object can have more than one prototype attribute (property)

Dynamic Properties

You define a primitive type value and a reference type value in a similar way: Create a variable and assign a value to the variable. However, when this value is saved to a variable, the operations that can be performed on different types of values are the same. For a value of a reference type, we can add properties and methods for it each page can remove the change and delete its properties and methods.

var person=new Object ();p erson.name= "Zhang San"; alert (person.name); var name= "John Doe"; name.age= "; alert (name.age);
The above code creates an object to save it in the variable person. Then we add a property named Name to the object and assign a value to the property, and when I print this property, I can get the result. This means that if the object is not destroyed or the property is not deleted, this property will persist. But we can't add attributes to the base data type.

Copy variable values:

In addition to preserving the different ways, there is a difference in copying the values of the base type and reference type from one variable to another. If the value of the base type is copied from one variable to another, a new value is created on the variable object and the value is copied to the location where the new variable is assigned;

var num1=5;var num2=num1;num1=3;alert (num2);

The value saved in this NUM1 is 5. When you initialize num2 with the value of NUM1, the value 5 is also saved in num2. However, 5 in num2 and 5 in NUM1 are completely independent. This value is just a copy of 5 in NUM1. Thereafter, these two variables can participate in any operation that does not affect each other. I think this is very similar to other high-level languages such as Java.

When a value of a reference type is copied from one variable to another, the value stored in the variable object is also copied to the space allocated for the new variable. The difference is that the copy of this value is actually a pointer, and this pointer points to an object stored in the heap, and after the copy operation ends, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other. This is related to the storage location of the reference type.

var obj1=new Object (), var obj2=obj1;obj1.name= "Zhang San"; alert (obj2.name);
The print is Zhang San, because Obj1 and Obj2 are two references, both of which refer to the Value object (); When we manipulate the value by reference, when our object has changed, the value of its reference has changed;

Pass parameters:

The parameters of all functions in JS are passed by value. The parameters in Java are thought to be passed by value, some people think it is passed by the parameter, but I have written a blog, which is specifically discussed in this, Java is actually passed by value. In other words, copying the values from the outside of the function to the parameters inside the function is the same as copying the value from one variable to another. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference variable is passed as a copy.

When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, in the concept of JS, an element in the arguments array). When a value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable is reflected outside the function.

function Add (num) {Num+=10;return num;} var count=20;var resule=add (count); alert (resule); alert (count);

The parameter count and the parameter num do not affect each other, except that NUM replicates the value of count;

function SetName (obj) {obj.name= "Zhang San";} var person=new Object (); SetName (person); alert (person.name);
The above code creates an object, saves its value in the person reference, and then calls the SetName function, passing the value to obj,obj the object referenced by this time is the same object as the person referencing it, so the property values are modified and displayed outside. Many programmers mistakenly think that the objects modified in the local scope are reflected in the global scope, which means that they are passed by reference, which is unrealistic;

function SetName (obj) {obj.name= "Zhang San"; Obj=new Object (); obj.name= "John Doe";} var person=new Object (); SetName (person); alert (person.name);
If it is passed by reference, we re-create an object for the reference, and assign a value, but the final result is Zhang San, proving that it is passed by value;








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Values for JavaScript primitives and reference types

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.