Js basic data type, reference data type

Source: Internet
Author: User

Data Type

1. The ECMAScript variable contains two different types of values: the base type value, the reference type value;

2. Basic type value: Refers to a simple data segment stored in the stack memory;

3. Reference type value: Refers to the object that is stored in the heap memory, meaning that the variable is actually a pointer, which points to the actual value in the memory heap;

Back to Catalog two ways to access

4. Basic type value: Access by value, operation is the value they actually saved;

5. Reference type value: Access by reference, when the query, we need to read the memory address from the stack, and then trace back to find the value stored in the heap memory;

Back to Catalog Two types of replication

Copy of the basic type variable:

When copying from a variable to a variable, a new value is created in the stack, and the value is copied to the location allocated for the new variable, and changing the source data does not affect the new variable (non-interference);

Replication of reference type variables:

Copy is a pointer stored in the stack, copy the pointer to the space in the stack of the new variable allocated, and the pointer copy and the original pointer execute the same object stored in the heap, after the copy operation ends, two variables will actually refer to the same object; so changing one of them will affect the other;

Back to Catalog passing of function parameters

1. The parameters of all functions in the ECMA are passed by value;

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 copied to a local variable at the address of the memory

Basic data Type Pass parameters

Funciton Addten (num) {

num+=10;

return num;

}

var count=20;

var result=addten (count);

alert (count);//20

alert (resullt);//30

The execution results are: 20 and 30. In this code, the variable count is passed as a parameter to the function Addten, which is equivalent to copying the value of the variable count to the parameter of the function Addten. At this point, the Addten parameter num can be seen as a variable inside the function. In the previous section of the code, it is equivalent to copying the values between two basic data type variables. The basic data type has its own independent memory address, so num and count have nothing to do with it, they are just equal values, and the value of count does not change after the function has finished executing. Results outside the function are assigned directly, so the value of result is 30 of the function.

Reference type Pass Parameters

function SetName (obj) {

Obj.name= "LSN";

}

var person=new Object ();

SetName (person);

alert (person.name);//lsn

The result of the execution is: LSN. In this code, the function SetName adds a property name to the Obj object and assigns the property a value of "LSN" because obj is a reference type, so this is where the reference type person is assigned to obj. That is, the person and obj refer to a memory address, so when the new attribute name is added to obj, the person outside the function changes, and the result of the PERSON.NAEM is the LSN.

Whether a reference-type pass-through parameter is passed a value or a reference

function SetName (obj) {

Obj.name= "ABC";

Obj=new Object ();

Obj.name= "BCD";

}

var person=new Object ();

SetName (person);

alert (person.name);//ABC

The result of the execution is: ABC. The difference between instance 3 and instance 2 is that it adds 2 lines of code to the function, adds a new property name to the Obj object and assigns the value to obj, defines it as a new object (new Object ()), and then name assigns a new value "BCD" after the new object is defined. At this point, if passed by reference, the last person object is automatically modified to point to a new object whose Name property is "BCD", but the last display is "ABC", which means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, this new obj becomes a local object within the function when Obj=new object () inside the function, which is automatically destroyed when the function is finished.

Back to Catalog Two kinds of variable type detection

1. The typeof operator is the best tool for detecting basic types;

2. If the variable value is nul or an object, typeof will return "object";

3. instanceof is used to detect the reference type, it can detect the specific, what type of instance;

4. If the variable is an instance of the given reference type, the instanceof operator returns true;

Js basic data type, reference data 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.