Understand the scope thoroughly!

Understanding the scope ##

Combining with the closures written last time, let's talk about scope while the iron is hot, hoping to help understand closures and scope.
Scope can determine whether there is accessibility to variables, objects, and functions from different parts of the code.
The scope in javascript is divided into: local scope, global scope (and then combined with closures, chain scope will be mentioned)
As the name implies, local scope: variables that cannot be read outside the function are defined inside the function A variable is called a local variable, and the scope of a local variable is local.

function btn(){
    
    
    var a ="1"
    console.log(a)//能取到
}
console.log(a)//不能取到

Global scope: Variables that can be read outside the function. Variables defined outside the function are called global variables. The scope of global variables is global. All scripts and functions on the page can access it.

var a ="1"
function btn(){
    
    
    console.log(a)//能取到
}
console.log(a)//能取到

Note: When you define a variable without using var inside a function, although this variable is inside the function, it is also a global variable and its scope is also global. as follows

function btn(){
    
    
    a ="1"
    console.log(a)//能取到
}
console.log(a)//能取到

You may ask, if a variable is defined in a function, how long can this variable survive and when will it be destroyed?
Answer:
Local variables: Local variables are created when the function starts, and when the function is executed, These local variables are destroyed.
Global variables: take effect when defined, and are destroyed when the page is closed.
Because of this: the same variable name can be used in different functions.
Note: As a function parameter (such as passing parameters), in fact, the variable within the function = local variable = scope is also local.

Talk about chain scope structure

In fact, the so-called chain scope is a phenomenon of closure.
Chained scope structure, the child will look up all the variables defined by the parent, so all the variables of the parent are open to the child (all the children can get it), but the parent is not Take the variable of the child.
In the following code, the a2 function is included in the a1 function, and the a2 function can get all the variables in the a1 function. But a1 cannot get the variables in the a2 function.

function a1(){
    
    
    var w="10"
    function a2(){
    
    
        var y="20"
        console.log(w)//可以拿到
    }
    console.log(y)//拿不到
}

In fact, looking at the above code, I am thinking about what a closure is: defining an internal function in an external function
is a way of writing a closure. What if we want the external function to get the variables defined in the internal function?
The solution was also discussed in the previous blog, as follows

function a1 (){
    
    
     var w3=10
     return function (){
    
    
       console.log("闭包3:",w3) 
     }
   }
  btn3()()

Return the internal function as an anonymous function.
I briefly talked about the scope. It is still very easy to understand. It is not easy to be original. I hope that friends who understand it can give a support and pay attention to it.

-Work hard and work hard!

Guess you like

Origin blog.csdn.net/weixin_47679798/article/details/109346226