JavaScript base types and reference types

Source: Internet
Author: User

Base type (null, Undefined, Boolean, number, string) and reference type (Object object)

1 Basic type: Only one value, one type, copy the value of the base type from one variable to another, creates a new value on the variable object, and then copies the value to the location assigned to the new variable, var num1 = 5;var num2 = num1; NUM1 and num2 are completely independent and operate independently of each other.

2 Reference type: Creates an object that can add, modify, delete properties and methods, and when a variable copies a reference type value to another variable, it also copies a copy of the value stored in the variable object into the space allocated for the new variable, but the copy of the 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 variable. var obj1 = new Object (); var obj2 = obj1; Obj1.name = "Summer"; alert (obj2.name);//' Output summer.

3 (function) parameter passing: the delivery of a primitive type is like a copy of a primitive type variable, whereas a reference to a value of a type is the same as a copy of a reference type variable. When passing a value of a primitive type to a parameter, the passed value is copied to a local variable, and when the value of the reference type is passed to the parameter, the value is placed in memory in the address to the local variable, so the change of the local variable will be reflected outside the function.

Instance A:

Functionaddten () {

Num +=10;

return num;

}

var count = 20;

var result = Addten (count);

Alert (cont);//20, no change.

alert (result);//30

Since the passing of the basic type value, the function parameters and external variables are independent from each other and are not affected;

Example B:

function SetName (obj) {

Obj.name = ' Summer ';

}

var person = new Object ();

SetName (person);

alert (person.name);//' Summer '

Because the value of the reference type is passed, the function argument and the external variable point to the same memory address and refer to the same object; in other words, even if the variable is passed by value, obj accesses the same object by reference. Therefore, when you add the Name property to obj inside a function, the person outside the function will also react because the object pointed to by person has only one and global object in the heap memory. Many developers mistakenly think that the objects modified in the local scope are reflected in the global scope, stating that the parameters are passed by reference, but see the following example,

Example C:

function SetName () {

Obj.name = ' Summer ';

obj = new Object ();

Obj.name = ' Winter ';

}

var person = new Object ();

SetName (person);

alert (person.name);//' Summer '

The difference between this and instance B is that the function has redefined an object for obj and reset the value of the Name property. If the person is passed by reference, the person is automatically modified to point to a new object whose Name property has a value of "winter", but the original reference remains unchanged. This means that when you rewrite obj inside a function, the variable refers to a local object, and the local object is destroyed as soon as the function finishes executing.

4 Detection type: To detect whether a variable is a basic data type, typeof is the best tool. When a value of a reference type is detected, use instanceof to return True or false. Such as

Alert (person instanceof Object);

Alert (colors instanceof Array);

Alert (patten instanceof REGEXP);

JavaScript base types 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.