Variable scope, the scope chain

1. Variables

1), the variable data containers are stored

2), the data type of the variable data types and the basic points of reference data types

Basic types: string "hello", number 110, null, undefined, Boolean true / false

 Reference Type: Object {}, array []

Difference: basic types of data can not be modified, stored in the stack memory; comparison, i.e. equal values are equal, the type of detection with typeof

    Reference types can be modified, stored in the heap memory; comparison, the same references are equal, with the type of detection instanceof

2. Memory: stack memory and heap memory

Stack memory stood fundamental variables, which are the same memory space, data ordered, LIFO

Heap stood a reference type referenced address, memory space inside big or small, the data is out of order, we need to look up the address

3. Scope

Global scope : After the destruction of all code been executed

Variables and functions declared in global scope, will be saved as the properties and methods of the window object

Local scope (scope of the function): useless function is destroyed

Block-level scope: for loops and if statements {} block belonging to the scope

Variables are not using var declared in a function are global variables, use the var statement is that local variables (local variables can then access the internal function)

2. Scope chain : Find out from the

var name='xm';
function fn(){
    var name='xh';
    var sex='male';
    function fn2(){
        name='xj';
        var age=19;
console.log(name); //xj
console.log(sex); //male
} 
Fn2 ();
} ,
Fn ();

 

 
 

Here, starting inside fn2 Find name and age, no longer find words fn function from the inside, it has been found window; it will stop searching after finding up

 
 

 

 

 

Guess you like

Origin www.cnblogs.com/lita07/p/12315387.html