Kshitiz Gupta
Kshitiz Gupta

Reputation: 23

JS nested functions

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 .

Upvotes: 1

Views: 142

Answers (1)

Billy Moon
Billy Moon

Reputation: 58619

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 
}); 

Upvotes: 2

Related Questions