0

I want to call a function that is in another function.

example for the functions:

function funcOne() {
     function funcTwo() {    // i want to call to this function
         //do something
     }
}

I need to call to funcTwo function, when I click on a button which is outside of these two functions

how can i do it?

4
  • 5
    You don't. That's how lexical scoping works. You'll have to restructure your code. Commented May 19, 2014 at 12:24
  • What he said, why dont you ask what you actually want to do? Commented May 19, 2014 at 12:25
  • @Fonzy couse that what i need. but ok, i will restructure my source. Commented May 19, 2014 at 12:26
  • The general consensus, is you can't nest functions like that. Commented May 19, 2014 at 12:31

4 Answers 4

2

No, You can't call unless you return that function.

Function2 is private to function1.

you use

function funcOne() {
    return {
     funcTwo :function() {    // i want to call to this function
         //do something
     }
   }
}

EDIT: Structuring code

function funcOne() {
   var funcTwo = function() {    // private function
        //do something
     }
    return {
      funcTwo : funcTwo      
   }
}

Now you can call it as:

funcOne().funcTwo()
Sign up to request clarification or add additional context in comments.

Comments

1

As you have it defined in your example, you can't. funcTwo is scoped inside of funcOne, so it can only be called from inside funcOne. You can assign funcTwo to a variable that is scoped outside of funcOne and that would work:

var funcRef;
function funcOne() {
    funcRef = function funcTwo() {

    }
}

In this case, funcRef would hold a reference and could be used, but that reference is only set once funcOne has been executed.

Comments

1

Reading some Douglas Crockford may help you understand...

Try recoding as:

function funcOne() {
   this.funcTwo = function() {
   }
}

I think you'd have to declare an instance of a funcOne object and then call the funcTwo method of that object. I'm a bit busy at the moment so I can't refine this answer at the moment.

10 Comments

+1 for this! I was reading continuous pop-up of answers but no one thought about creating a simple constructor....to add...for OP....all he needs to do is ..var f= new funcone(); f.funcTwo()//whatever
i think that its what i need. but how can i call to funcTwo? <code>funcOne().funcTwo();</code>?
This actually is.. horrible. You'd hope that funcOne gets executed in the global scope and therefore, write to the global scope by accessing this. Never ever do that.
@jAndy: Can you care to explain little more, please?
Crockford himself states that if a function is intended to be called as a constructor (using "new") then the name of that function should be capitalized, to make sure that callers avoid the problems @jAndy mentioned.
|
0

It is not possible as the second function will be created just when the first function is called. it is not existent prior to that.

You would have to define it outside the first function like so:

function funcOne() {

}
function funcTwo() {    // i want to call to this function
    //do something
}

Or you could also call the first function and return the second function like this:

function funcOne() {
     function funcTwo() {    // i want to call to this function
         //do something
     }
     return functTwo;
}

And then call it like this:

var f = funcOne();
f();

Comments

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.