closures in js
A closure in JavaScript is known as JavaScript closure. It makes it possible for a function to have "private" variables.
Whenever you create a function within another function, then the inner function is closure.
This closure is usually returned so you can use the outer function’s variables at a later time.
scope in JavaScript defines what variables you have access to. There are two kinds of scope – global scope and local scope.
function f1(){
count=0;
return function f2(){
count++;
return count;
}
f2();
}
let A=f1();
console.log(A());
console.log(A());
........................................
function f1(){
var a=7;
function f2(){
console.log(a);
}
f2();
}
f1();
//function bind togther
// with in its lexical environment
//function with its lexical scope forms a closure.
/*
A lexical scope in JavaScript means that a
variable defined outside a function
can be accessible inside another
function defined after the variable declaration
*/
....................................function f1(){
var a=7;
function f2(){
console.log(a);
}
return f2;
}
var k=f1();
console.log(k);
Comments
Post a Comment