JS value types and reference types

Source: Internet
Author: User

Let me introduce you to another interpretation of data types, value types and reference types (and simple types and composite types)

First, the basic concept

The ECMAScript variable consists of two different data type values, one of which is called a simple type (value type), refers to a simple data segment, and another is called a composite data type (reference type), which is an object consisting of multiple values.

In JS data types, both number,boolean,string,null and undefined are value types, and functions and objects are reference types.

Second, the data type judgment

Base type: typeof

Reference type: instanceof

vara=10; Console.log (typeofa);// Numbervarb=true; Console.log (typeofb);//BooleanvarArr=[];console.log (arrinstanceofArray);//truevarobj={};console.log (objinstanceofObject);//true;varfun=function(){  varI=0;            Console.log (i); }console.log ( FuninstanceofFunction)//true
Third, storage mechanism

Basic data type: is stored in the stack memory, including the variable identifier and the value of the variable, the basic data type is copied after the new memory, two variables do not affect each other.

Reference type: In heap memory, it is a reference (and pointer) to the address, so the two variables that are copied by the variable point to the same object, so one is manipulated and another result is affected.

Let's give you some examples.

varnum1=10;varNum2=num1;num2-=2; Console.log (num1,num2);//10,8vararr1=[1,2,3];varArr2=Arr1;arr2.push (4); Console.log (ARR1,ARR2);//[1,2,3,4],[1,2,3,4]varperson1={name:' Zhangsan ', Age:20}varPerson2=Person1;person2.name= ' Lisi '; Console.log (ARR1,ARR2);//{name: ' Lisi ', Age:20},{name: ' Lisi ', age:20}

Iv. methods for creating object instances

1, use the new operator followed by the object constructor

2, literal

var obj=New Object (); // or var obj2={};console.log (obj,obj2); // {},{}     console.log (OBJ==OBJ2)  false  heap memory, different address /get // and dot notation and square brackets notation  var person1={name:' Zhangsan ', Age:}console.log (person1.name)//  Zhangsanconsole.log (person1[' name ')//Zhangsan

PS: The difference between dot notation and square brackets notation

1, can be identified by the dot notation can be expressed in square brackets, but can be expressed in square brackets, not necessarily can be used to indicate the point

2, the square bracket notation can be used as the name of the variable name, dot notation cannot

3, the square brackets can be represented by a pure number of attributes, dot notation cannot be

4, square bracket notation can use JS keyword and reserved word as attributes, dot notation cannot

Here I do not want to give you an example, here is still very well understood, in short, the square bracket notation is stronger than the point notation, but generally we use the dot notation to pay attention to the special is good.

JS value types and reference types

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.