0

I have a complex code.this is its sample.i can not change structure. how can i call e() in f()?

a=function b()
    {
        var c,d;
        c=function()
        {
              function e()
              {
              ...
              }
              ...
        }
        d=function()
        {
              function f()
              {
              //i need e() here
              }
              ....
        }

}
4
  • 4
    You can't. e is local to c, so why would you need it? Commented Oct 21, 2012 at 11:59
  • How are a, d and f called? How is c called? Commented Oct 21, 2012 at 12:02
  • thank u and it's complicated code and i want to change it and i cant explain it easily. Commented Oct 21, 2012 at 12:02
  • It can't be that complicated, try it. Do you use closures? Commented Oct 21, 2012 at 12:06

2 Answers 2

2

You can't as those are local functions within different scopes and are not accessible from each other. If you need to call e() from inside f()'s scope, then e() shouldn't be defined within c()'s scope, but within b(), where it will be visible in f()'s scope.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't. That's the whole point of scoping.

You can, however, make the variable e available to the parent scope by declaring it outside the function c.

a = function b() {
  var c, d, e;
  c = function () {
    e = function () {
      ...
    };
    ...
  };
  d = function () {
    function f() {
      e();
    }
    ...
  };
};

4 Comments

That would need c to have been called before d/f, which could be confusing.
what if i have one e() in d() and another in c(). i have to change its name?
No, just call it how you normally would: e() in both d and c
just same name with different functionality

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.