JavaScript -> the difference between basic types and reference types

First look at the basics of array: (an array of objects belonging to reference types) with code

<! DOCTYPE HTML> 
<HTML lang = " EN " > 
<head> 
    <Meta charset = " UTF-. 8 " > 
    <title> the Document </ title> 
</ head> 
<body> 
    <Script type = " text / JavaScript " >
         // array: memory to store a set of variables in a continuous memory space to store more data
         // using an array of benefits: a plurality of data stored contiguously easy to maintain and query
         // create an array:
         // 1. create an empty array : 
        var ARR = [];
         var of arr1 = new new the Array ();
         // 2.Create a known number of empty array data 
        var arr2 is =new new Array ( 29 );
         // 3. create an array of simultaneous assignment 
        var arr = [ " Wang Wu " , " John Doe " , " Joe Smith " ];
         var arr1 = new new Array ( " Wang Wu " , " John Doe " , " Joe Smith " );
         // by accessing the array index, the value is displayed if no undefined 
        the console.log (ARR [ 0 ], ARR [ . 1 ], ARR [ 2 ], ARR [ . 3 ]);
         // features:
        @ 1 does not limit the types of data elements stored 
        var Array = [ " Jack " , to true , 3.14 ];
         // the subject 2 is not limited bounds 
        Array [ . 4 ] = 80 ; // automatically developing new element holds the position of the element 
        the console.log (array); 
        the console.log (array [ . 3 ]);
         // number of elements in the array is not limited to 3. 
        array [ 10 ] = " Marry " ; 
        the console.log (array); 
        // 4. array .length property: record the number of elements in theory always is the index of the last element of +1 
        console.log (array.length); //1 + 10
         @ 5 acquired the last element of the array-1 be array.length 
        the console.log (Array [array.length- 1 ]);
         // 6. The append new data to the end of be array.length 
        Array [be array.length ] = " haha " ; 
        the console.log (array); 
        // delete an element array 
        array [ . 1 ] = "" ;
         // delete the last element of the array 
        array.length-- ;
         // delete end of the array of n elements 
        be array.length array.length- = n;
     </ Script> 
</ body> 
</ HTML>

 

Basic types: Number The String null undefined boolean

Data type data directly stored in the variable locally
when data transfer value is the value of the original copy of the variable passed to a new variable, the value of the new variable is changed, the value of the original variable will not change
the reference type:
1. data types of data can not be stored directly in a local variable, the variable only allows you to store a value of
2. reference object type would create an independent outside the window object storage space, and give a unique address, saved only on this variable address memory space - referred to as "reference"
3. data value transfer, the transfer address, when the modified new variable, the data will change

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-. 8"> 
    <title> array is a reference to an object type </ title> 
</ head> 
<body> 
    <Script type = "text / JavaScript"> // change will not affect the new variable value passed when the original base-type variable var a = 10 ;
         var B = a; 
        B ++ ; 
        the console.log (a); // value 10 
        the console.log (B); // is 11 // reference type passaged value, the new variable changes directly changes the value of the original variable as address transfer between the data objects stored var ARR = [. 1, 2,3,4,5 ];
         var of arr1 = ARR; 
        of arr1.length--;
        
        
        
        
        the console.log (String (ARR)); // 1,2,3,4 
        of arr1 = null ; // When the assignment is empty, this indicates that the variable is no longer referenced array 
        the console.log (String (of arr1)); 
        
        / / undefined variable is defined using JS assignment, the initial value of the variable is undefined 
        // null programmer, manually clear the contents of a variable 
            // basic types: direct emptied the contents of variables 
            // reference type: This variable is no longer referenced objects 
        / / I Shiqing empty a variable 
        // garbage collection: js engine will automatically release the object is no longer referenced by any variable 
        // garbage collector: specialized recycling unused objects applet 
            // 1. record each object is with reference to several variables, each of a plurality variable reference, counter + 1 
            //      when a variable assignment null, the counter -1 
            // 2. when the counter reaches 0, it means the object is no longer referenced by any variable object is recovered 
            // 3. active emptied using a large object 
    </ Script> 
</ body> 
</ HTML>

 

Guess you like

Origin www.cnblogs.com/blogzzy/p/11247092.html