Java Reference
In-Depth Information
function temple(){
var weapons = "Katana, Bo, Shuriken, Yuri";
function innerSanctum(){
var stealth = "Cho Ho, Shinobi-Iri, Henso-Jutsu";
}
}
Outside the
temple
function, the
weapons
and
stealth
variables are unable to be ac-
cessed. Inside the
temple()
function, the
weapons
variable can be accessed, and inside
the
innerSanctum()
function, both the
weapons
and the
stealth
variable can be
accessed.
A ninja who leaves the temple will still have all the skills that were learned there and be
able to use them outside the temple. This is the concept of a closure: if a value or function
is returned from a function, it maintains the references to the scope in which it was cre-
ated. In the example, we can return the
innerSanctum()
function that has access to the
weapons
and
stealth
variables:
function temple(){
var weapons = "Katana, Bo, Shuriken, Yuri";
function innerSanctum(){
var stealth = "Cho Ho, Shinobi-Iri, Henso-Jutsu";
return "Ninja Skills " + weapons + " " + stealth;
}
return innerSanctum();
}
When the
temple()
function is called, it can be assigned to a
ninja
variable that will
have access to the information contained in the
weapons
and
stealth
variables outside
the
temple()
function:
ninja = temple();
<< "Ninja Skills Katana, Bo, Shuriken, Yuri Cho Ho,
