The basic variable types and reference types

Javascript variables into the basic variables and references.

  • Basic types : as undefined, null, Number, String, Boolean, Symbol ... accessed by value, the actual value stored in the operational variable, stored in the stack memory.
  • Reference Type : The plurality of objects may be composed of a plurality of values, arrays, ... regular access by reference, not directly access object memory operation stored in the stack memory and heap memory.

1. Copy the variable

  • Basic types : create a new value, the value is copied to the newly allocated variable address, two values may participate in any operation without affecting each other.
  •  

     

  • Reference types : create a new pointer (pointer to object storage pile to be copied), the pointer into the stack space allocated for a new variable in
  •  

2. parameter passing

All parameters in ECMAScrpit are passed by value of. The basic types and reference types are passed as parameters similar to replication.

  • Basic types : value is passed will be copied to a local variable in a function, local variables inside a function does not affect the value of a function of the external variables.
  • Reference types : the value of the memory address (directed into the heap data) copied to the local variables inside a function (in this case outside the variable local variables and function data of the same point within the stack), the modified local scope value in global reflection.

3. Check Type

  

       was num = 1 ;
        was pc = 'ha ha' ;
        was stay = true ;
        was now = null ;
        was obj = new Object ();
        where s = [];

 

  • typeof : typeof operator is determined that the value is a Number, Boolean, undefined, String very good tool, but the basic type or a reference type is determined not so easy to use;

  

     console.log(typeof(num));//Number
        console.log(typeof(st));//String
        console.log(typeof(bo));//Boolean
        console.log(typeof(obj),typeof(nu));//Object,Object

 

  • instanceof: If the reference type is detected instanceof instances returns true, false returns the basic type
        console.log(nu instanceof Object );//false
        console.log(obj instanceof  Object);//true
        console.log(ar instanceof Array );//true

 

 

Reference: "JavaScript The Definitive Guide," third edition

Guess you like

Origin www.cnblogs.com/ellen-mylife/p/11258333.html