Understanding scope# #

I’ll talk about scopes based on the closure I wrote last time, and hopefully help understand closures and scopes. A scope determines whether variables, objects, and functions are accessible from different parts of the code. In javascript, scopes are divided into: local scope Global scope as the name suggests, local scope: is a variable that can not be read outside the function, inside the function to define a variable called a local variable, the scope of the local variable is local.

function btn(){
    var a ="1"
    console.log(a)/ / to get to
}
console.log(a)// Can not fetch
Copy the code

Global scope: a variable that can be read outside a function. A variable defined outside a function is called a global variable. The scope of a global variable is global. It is accessible to all scripts and functions on the page.

var a ="1"
function btn(){
    console.log(a)/ / to get to
}
console.log(a)/ / to get to
Copy the code

Note: When you define a variable inside a function without using var, it is a global variable inside the function and its scope is global. The following

function btn(){
    a ="1"
    console.log(a)/ / to get to
}
console.log(a)/ / to get to
Copy the code

If you define a variable in a function, how long does it last and when does it get destroyed? A: Local variables: local variables are created at the start of a function and destroyed when the function is finished. Global variables: effective when defined and destroyed when the page is closed. Because of this, you can use the same variable name in different functions. Note: parameters to a function (such as passing arguments) are actually variables inside the function = local variables = scope is also local.

Talk about the chain scope structure

In fact the so-called chain scope is a phenomenon of closures. In the chained scope structure, the child items will look up all the variables defined by the parent item layer by layer, so all variables of the parent item are open to the child item (the child items can be retrieved), and the parent item cannot take the variables of the child item. In the following code, the a2 function is contained in the a1 function, and the A2 function takes all the variables in the A1 function. But A1 can’t get the variables in the a2 function.

function a1(){
    var w="10"
    function a2(){
        var y="20"
        console.log(w)// You can get it
    }
    console.log(y)/ / can't get
}
Copy the code

If you want an external function, you can get the variables defined in the internal function. The solution was also described in the last blog, as follows

function a1 (){
     var w3=10
     return function (){
       console.log(3: "closure",w3) 
     }
   }
  btn3()()
Copy the code

Return the inner function as an anonymous function. Simple about the scope, or very easy to understand, the original is not easy, hope to understand the friends to give a support to a concern ~~~ three grams of oil.

— Try, try, try again! Front-end communication group (866270649) share knowledge and make progress together.