Basic types and reference types in JS

Source: Internet
Author: User

Transfer from https://www.cnblogs.com/gromimiss/p/6066268.html

Basic data type: accessed by value to manipulate the actual value saved in the variable. The base type value refers to a simple data segment.

There are five kinds of basic data types: Undefined, Null, String, number, Boolean.

Reference type: When you copy a variable that holds an object, the action is a reference to the object, but when you add a property to the object, the actual object is manipulated. Reference type values refer to objects that may be composed of multiple values.

There are several reference types: object, Array, RegExp, Date, Function, Special basic wrapper type (String, number, Boolean), and monolithic built-in objects (Global, Math).

For the difference between the base type value and the reference type value:

① reference type values can add properties and methods, while primitive type values are not.

Add a property for a reference type value var p = new Object ();p. Age=11;alert (p.age);//11//adds an attribute to the base type value var name = ' a '; name.age = 11;alert (name.age); Undefined

② when you copy a variable value, the base type creates a new value on the variable object and then copies it to the new variable. Thereafter, any action of two variables does not affect the other, whereas the reference type copies the value stored in the variable object to the new variable, but the values of the two variables point to an object stored in the heap, that is, they refer to the same object, changing one of the variables will affect the other.

The base type value var a = ' a '; var b = a;a = ' B '; alert (b); A

Reference type values, as an example of an array:

The reference type value, in the array as an example//1. Assigning a value directly to one of the variables does not affect another variable (the referenced object is not manipulated) var a = [1,2,3];var b = A;a = [1,2,3,4];alert (a);//1,2,3,4alert (b); 1,2,3//2. Using push (manipulating referenced objects) var a = [1,2,3];var B = A;a.push (4); alert (a);//1,2,3,4alert (b); 1,2,3,4

Pass parameters: Pass by value, copy the value outside the function to the parameter inside the function (a local variable), and when the local variable is manipulated, the change of the local variable will be reflected outside the function, but this does not affect the value outside the function.

function Add (a) {    A + = ten;    return A;} var num = 10;var result = Add (num); alert (num); 10alert (result); 20

Of course, using objects can be difficult to understand:

function SetName (obj) {    obj.name = ' A ';} var p = new Object (); SetName (P); alert (p.name); A

The argument is passed by value, why is the P instance created can also get the name attribute added in SetName ()?

Because obj and P refer to the same object, obj accesses the same object by reference, even if it is passed by value. Take a look at the following example to make it clear.

function SetName (obj) {    obj.name = ' a ';    obj = new Object ();    Obj.name = ' a ';    return obj;} var p = new Object (), var p2 = setName (p); alert (p.name); Aalert (P2.name); B

Overrides the Obj object inside the function, at which point the Obj object refers to a local object, and the external p is the original reference and therefore does not change.

Detection type:

typeof: Determines whether a variable is the best tool for a string, numeric, Boolean, or undefined.

var num = 1;var A = ' a '; var b;var flag = True;var o = null;alert (typeof num); Numberalert (typeof a); Stringalert (typeof B); Undefinedalert (typeof flag); Booleanalert (typeof O); Object

Instanceof: Determines whether an object type.

var a = [1,2,3];alert (a instanceof Object); Truealert (a instanceof Array); Truealert (a instanceof RegExp); False

Basic types and reference types in JS

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.