javascript closure simple explanation
What is Closure?
A closure is that the combination of a function and therefore the lexical environment within which that function was declared.
A closure is an inner function that has access to the outer (enclosing) function’s variables — scope chain. The closure has three scope chains: it's access to its own scope (variables defined between its curly brackets), it's access to the outer function’s variables, and it's access to the worldwide variables.
Let see a closure example below:
function User(name){ var displayName = function(greeting){
console.log(greeting+' '+name);
}
return displayName;
}var myFunc = User('Raj');myFunc('Welcome '); //Output: Welcome Raj
myFunc('Hello '); //output: Hello Raj
In this code, We have an outer function User()
which returns an inner function as displayName()
,
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
0 Comments