JS嵌套函数

时间:2013-02-21 10:08:38

标签: javascript

我正在使用嵌套函数

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

我想要的是在anystring中访问该值,如

var fetchvalue ;

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

如果我走在正确的轨道上,请指导我。

1 个答案:

答案 0 :(得分:2)

稍微整理你的代码,纠正拼写错误等......,并观察mainfunction的输出为你提供这个工作脚本。很难判断这是否回答了你的问题,但它确实将一个变量发送给回调函数,然后从该回调中获得一个返回值。

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