function Hello()
{
function HelloAgain(){
}
}
function MyFunction(){
...
}
Here I need to call the function HelloAgain() from MyFunction(). Can you suggest any possible solution for this scenario.
function Hello()
{
function HelloAgain(){
}
}
function MyFunction(){
...
}
Here I need to call the function HelloAgain() from MyFunction(). Can you suggest any possible solution for this scenario.
You can't, the HelloAgain function is in a closure and is scoped to that closure.
You could return the HelloAgain function and then call it from anywhere you can call Hello.
function Hello () {
function HelloAgain () {}
return HelloAgain;
}
function MyFunction () {
Hello().HelloAgain();
}
But that slightly weird.
More over you could use the this
keyword to deal with the matter.
function Hello () {
this.helloAgain = function helloAgain () {}
}
var hello = new Hello();
function MyFunction () {
hello.helloAgain();
}
You can't. The scope of HelloAgain
il limited to the Hello
function.
This is the only way to make private scope in javascript.