JS object-oriented - you really understand closures yet?

JS closures, less likely in the actual development we use, but the interview will be asked.
Today, we summarize what is closure.

First, we define a variable. Will be divided into two cases, one is defined, when we close the program variables is released from memory in the big picture. 2 is defined in the local, the definition of a variable in the function, after the end of my function call is released from memory.

There is a closure, it is to call a function when I declare local variables, local variables I can exist in the larger whole. I extend the life cycle of local variables.

Then write a simple closure ~~

function aaa(){
   var a =10
    function bbb(){
      console.log(a)   
   }
  return bbb
}

var num = aaa()
num()

  

So it is how to judge a program closures have it?
1. Is there a function of the outer and inner function eg: AAA () BBB ()
2. outer function if there are local variables eg: var A = 10
3. inner function whether the operator of the local variable outer function eg: console.log (a)
whether the outside and 4. the inner layer had a correlation function eg: return bbb

Thank you see here, we hope this article has helped ~~~

Guess you like

Origin www.cnblogs.com/JiAyInNnNn/p/11070274.html