Java Reference
In-Depth Information
Shinobi-Iri,
Henso-Jutsu"
The ninja variable represents a closure, as it has access to the variables that were created
inside the temple() function.
A Basic Closure Example
A simple closure involves returning the value of a variable that was created inside the func-
tion's scope to make it available outside the function:
function closure(){
var inside = "I was created inside the function";
return inside;
}
The value of the inside variable is unavailable outside the closure() function:
inside;
<< Error: "inside is not defined"
But we can create a closure by invoking the closure() function, which will return the
value of inside and make it available outside the function:
outside = closure();
<< "I was created inside the function"
Returning a Function
A function can form a closure that maintains access to all the variables created in the ori-
ginal function's scope by returning a function instead of a single value:
function closure() {
var a = 1.8;
var b = 32;
return function(c){
Search WWH ::




Custom Search