0

Could everyone tell me how to call a function inside a function? For example :

 function betterExampleNeeded() { 
 var a = 1; 
 function oneMoreThanA() { 
 return a + 1; 
 } 
 return oneMoreThanA(); 
 } 

How to call oneMoreThanA( ) Thanks in advance

asked Mar 23, 2015 at 20:12
5
  • 5
    Just invoke the function betterExampleNeeded Commented Mar 23, 2015 at 20:13
  • 1
    What's wrong with the way you call oneMoreThanA() in your sample? Commented Mar 23, 2015 at 20:17
  • I try to call oneMoreThanA() directly in Javascript Console in Chrome. It returns an error Uncaught ReferenceError: oneMoreThanA is not defined Commented Mar 23, 2015 at 20:21
  • oneMoreThanA is a privatemethod so it can not be called from outside. But betterExampleNeeded() function is visible from outside and returns a reference to your method. So you just need to invoke betterExampleNeeded() to run oneMoreThanA() method. Commented Mar 23, 2015 at 20:30
  • Thanks for response. Can I invoke oneMoreThanA() by using betterExampleNeeded() .oneMoreThanA()? Commented Mar 23, 2015 at 20:34

1 Answer 1

1

You're calling oneMoreThanA in your example.

If you want to call it from outside your betterExampleNeeded function, then you'll need to have betterExampleNeeded make the functino reference available outside of it, by:

  • Returning it
  • Assigning it to a variable in the containing scope
  • Assigning it to an object property on an argument passed into it

...or similar.

For instance:

function betterExampleNeeded() { 
 var a = 1; 
 function oneMoreThanA() { 
 return a + 1; 
 } 
 return oneMoreThanA; // <=== Note! No ()
 } 
 var f = betterExampleNeeded();
 console.log(f()); // 2
 console.log(f()); // 2
 console.log(f()); // 2

Or we could even modify a:

function betterExampleNeeded() { 
 var a = 1; 
 function oneMoreThanA() { 
 return ++a; // <=== Modify `a`
 } 
 return oneMoreThanA;
 } 
 var f = betterExampleNeeded();
 console.log(f()); // 2
 console.log(f()); // 3
 console.log(f()); // 4
answered Mar 23, 2015 at 20:21
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for response. I got some problems in understanding javascript. Please correct me , if I am wrong. A function is a object, if I assign this function to variable. For example, var a = function betterExampleNeeded() { var a = 1; function oneMoreThanA() { return a + 1; } return oneMoreThanA(); } can I call the function inside by using a.oneMoreThanA() ? Apologies for my format, I dont know how to adjust here.
sorry, I should have removed "betterExampleNeeded" to make it a anonymous function
@XuzhengWang: No, because although functions are objects, you haven't set a property on that function with the name oneMoreThanA. The oneMoreThanA inside that function is completely private to code within the function, unless (again) you do one of the things listed above to expose it to the outside world.
@XuzhengWang: Or to put it another way: A function declared inside another function is very much like a local variable. So just like local variables don't become properties of the function instance, functions declared inside them don't either.
Thanks a lot! I got confused about the difference between var a = function ( ){} and var a = { }.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.