In the first article, I want to take notes about closures and tidy up my thoughts
What is a closure?
One thing to note is that you need to use the var statement to define variables inside a function, or declare a global variable if you don’t.
var a=1; var F = function(){ var b=2; var N = function(){ var c=3; }; };
a
b
c
a
b
closure
a
b
a
var
var a=1; var F = function(){ var b=2; var N = function(){ var c=3; return b; }; return N; };
>b Uncaught ReferenceError: b is not defined >var f=F(); >f(); 2
var f; var F = function(){ var b=2; var N = function(){ var c=3; return b; }; f=N; };
>f(); 2
What’s the use of closures?