javascript-the difference between a basic type and a reference type

Source: Internet
Author: User

When looking at JS Advanced Program design, found that this part although simple, but I myself some places often confused, summarized as follows:

(i) Basic concepts

JS can be divided into two parts, the basic type and the reference type.


The basic types are simpler, including: Undefined, Null, Boolean, number, and string, which are simple data segments and objects that reference type values may consist of multiple values.

Reference type values are stored in memory, and JS is not directly accessible to memory, so for reference types, the operation is not the actual object but the reference to the object.

(ii) The difference between a primitive type value and a reference type value

A, dynamic properties

You can only add properties to the value of a reference type dynamically, for example:

var New  = "Zhangsan"//Zhangsan

However, you cannot add attributes to the values of the base type, although no errors are given, for example:

var name= "Zhangsan";
Name.age=21;
// undefined

B. Copy variable values

When you copy a base type value from one variable to another, a new value is created on the variable object, and the values in the two variables are independent of each other. Examples are as follows:

var num1=5; var num2=num1;

The image shows the process of copying a basic type value:

Num1

5 (Numbei type value)

The variable object before copying

Num2 5 (Numbei type value )
Num3 5 (Numbei type value)

The copied variable object

When you copy a reference type value from one variable to another, a new value is also created on the variable object, unlike the new value, which is a pointer, so the copy

At the end, two variables actually refer to the same object. Changing one will also affect the other, for example:

var obj1=New  Object (); var obj2=obj1;obj1.name= "Zhangsan"//Zhangsan

Shows the relationships between the objects that are saved in the variable and the objects that are saved in the heap:

c, passing parameters

The parameters of all functions in JS are passed by value. The following three examples are a good illustration of this:

(1) Passing a base type value to a parameter

function Addten (num) {      num+=10;       return num;      } var count=20; var result=Addten (count); alert (count);   // alert (result);  //  -

Because the parameter is actually a local variable of the function, the change in the function's internal parameters does not affect the count variable outside the function. The parameter num and count are independent of each other.

(2) Passing an object to a parameter (object is a reference type value)

function setName (obj) {      obj.name= "Zhangsan";} var person=New  Object (); SetName (person); alert  (Person.name) ; // Zhangdan

The above code creates an object and saves it in the variable person. The variable is then passed to the SetName () function and copied to obj.

Inside the function, obj and person refer to the same object. Then, when you add the Name property to obj inside the function, the person outside the function reacts.

Therefore, we are misled by the arguments that are passed by reference.

(3) Passing an object to a parameter, making a change to the previous example further illustrates that the parameter is passed by value.

function setName (obj) {      obj.name= "Zhangsan";      obj=new  Object ();      Obj.name= "Greg";} var person=New  Object (); SetName (person); alert  (Person.name) ; // Zhangdan

In the above example, the value of person is not changed by the modification of obj, and the description is not passed by reference. If it is passed by reference, obj and person point to the same object, and when obj changes, the person changes.

D, detection type

typeof is used to detect simple types, and for objects, we would like to know what type of object. So the instanceof operator was introduced.

For example: alert (person instanceof Object); is the variable person an object type? If it returns true, it is not returned false.

Note: All reference types are object instances, and instanceof always returns True when a reference type value and an object constructor are instrumented. Instead, instanceof detects the value of the base type and always returns false because

The base type is not an object.

These are the differences between the base type and the reference type.

javascript-the difference between a basic type and a reference type

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.