1

I am using nested functions

Function mainfunction (callbackfun) {
 //some code + function (score)
 {
 var score = scoreString;
 alert(scoreString);
 callbackFun(score);
 }
} //--> I return this value to my calling function
mainfunction(function (anystring){
 alert(anystring); //-> this would return me the value in callbackfun 
}); 

What I want is access that value in anystring out like

var fetchvalue ;
mainfunction(function (anystring){
 fetchvalue =anystring; //-> this would return me the value in callbackfun 
}); 

Please guide me if am on the right track .

BenMorel
36.9k52 gold badges208 silver badges339 bronze badges
asked Feb 21, 2013 at 10:08
2
  • 1
    Have you tried it? Just a tip: JS is case sensitive, so it should be function instead of Function and your parameter is named callbackfun, not callbackFun. With this fixed, this is what your code is doing: jsfiddle.net/P3bBP. Is this what you want? Commented Feb 21, 2013 at 10:11
  • 2
    What is the question? What are you trying to achieve with your code? Edit: ok, after editing the question is visible ;-) Commented Feb 21, 2013 at 10:12

1 Answer 1

2

Tidying up your code a bit, correcting spelling errors etc..., and watching the output of the mainfunction gives you this working script. It is hard to tell if this answers your question, but it does send a variable to a callback function, and then get a return value from that callback.

function mainfunction(callbackfun){
 //some code + function (score)
 var scoreString = Math.random()*10000000
 var score = scoreString;
 alert(callbackfun(score));
}; // --> i return this value to my calling function
mainfunction(function(anystring){
 return anystring; //-> this would return me the value in callbackfun 
}); 
answered Feb 21, 2013 at 10:14

Comments

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.