Talking about javascript closures

function a(){//outer function
  var n = 0;
  function inc() {//inner function
    n++;
    console.log(n);//output function
  }
  inc();//call inner function
  inc ();
}
a();
//The way it looks after decomposition;
/* Functions that have access to variables in the scope of another function are closures. Here the inc function accesses the variable n in the constructor a, so it forms a closure.
* $(function(){
a();//Call the outer function
})
function a(){//The outer function
var n = 0;
inc();//Call the inner function
  inc();
}
  function inc() {//Inner function
n++;
console.log(n);//Output function
  }*/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326907857&siteId=291194637