1
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.

Rahul S
  • 73
  • 2
  • 7

2 Answers2

1

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();
}
Vishal Sakaria
  • 1,367
  • 1
  • 17
  • 28
0

You can't. The scope of HelloAgain il limited to the Hello function. This is the only way to make private scope in javascript.

ema
  • 5,668
  • 1
  • 25
  • 31