Javascript调用子函数并从函数内部获取返回

Javascript调用子函数并从函数内部获取返回,javascript,function,Javascript,Function,在一个很长的函数中,我需要在继续脚本之前获得一个用户选项 我们的想法是通过如下方式获得choosen值('a'或'b'): function myMainFct { // a lot of lines here if(a_Defined_Value_Is_Ok) { var option="a"; } else { var choose = "<a onclick=\"myOption('a')\">A</a><a onclic

在一个很长的函数中,我需要在继续脚本之前获得一个用户选项

我们的想法是通过如下方式获得choosen值('a'或'b'):

function myMainFct {

  // a lot of lines here

    if(a_Defined_Value_Is_Ok) { var option="a"; }

    else {
      var choose = "<a onclick=\"myOption('a')\">A</a><a onclick=\"myOption('b')\">B</a>";
      // here I put the <a> choices in a div

        // now the subfunction
        function myOption(x) { return x; }

        // what will happen when I'll get the value
        if (x)  { var option=x; }
    }

    if (option=="a") {
      // let's continue the script for users with "a" option
    }

    else if (option=="b") {
      // let's continue the script for those with "b" option
    }

    // end of common main function
} 
函数myMainFct{
//这里有很多台词
如果(定义的值是确定的){var option=“a”;}
否则{
var choose=“AB”;
//在这里,我把选择放在一个div中
//现在是子功能
函数myOption(x){return x;}
//当我得到值时会发生什么
如果(x){var option=x;}
}
如果(选项==“a”){
//让我们继续使用“a”选项为用户编写脚本
}
否则如果(选项==“b”){
//让我们继续为那些有“b”选项的人编写脚本
}
//公共主函数结束
} 

符合您的语法:

var x = (function myOption(x) { return x; })();

您需要将选项处理代码放在单独的函数中

function myMainFct {

  //  a lot of lines here

  if(a_Defined_Value_IsOk) 
    myCompletion("a");

  else {
    var choose = "<a onclick=\"myCompletion('a')\">A</a><a onclick=\"myCompletion('b')\">B</a>";
    // here I put the <a> choices in a div

  }
  // end of common main function
}

function myCompletion(option) {

  if (option=="a") { 
    // let's continue the script for users with "a" option
  }

  else if (option=="b") {
    // let's continue the script for those with "b" option
  }

} 
函数myMainFct{
//这里有很多台词
if(定义的值)
我的补充(“a”);
否则{
var choose=“AB”;
//在这里,我把选择放在一个div中
}
//公共主函数结束
}
函数myCompletion(选项){
如果(选项==“a”){
//让我们继续使用“a”选项为用户编写脚本
}
否则如果(选项==“b”){
//让我们继续为那些有“b”选项的人编写脚本
}
} 

我终于找到了另一个解决方案。这不是最好的,但很管用

function myOption(x) { myMainFct(x); } // return to main function when called

function myMainFct(x) {

  // a lot of lines here

  if(a_Defined_Value_Is_Ok) { var option="a"; }

  else {
    var choose = "<a onclick=\"myOption('a')\">A</a><a onclick=\"myOption('b')\">B</a>";
    // here I put the <a> choices in a div

    if (x)  { var option=x; }
  }

  if (option=="a") {
    // let's continue the script for users with "a" option
  }

  else if (option=="b") {
    // let's continue the script for those with "b" option
  }

  // end of common main function
} 
函数myOption(x){myMainFct(x);}//调用时返回主函数
函数myMainFct(x){
//这里有很多台词
如果(定义的值是确定的){var option=“a”;}
否则{
var choose=“AB”;
//在这里,我把选择放在一个div中
如果(x){var option=x;}
}
如果(选项==“a”){
//让我们继续使用“a”选项为用户编写脚本
}
否则如果(选项==“b”){
//让我们继续为那些有“b”选项的人编写脚本
}
//公共主函数结束
} 

感谢您的回答,但我需要在主函数中继续执行脚本,因为如果函数中的(a)或(b)更进一步,我将不得不调用一些其他条件。。。所有这些都可能发生在延拓函数中。您还可以将完成函数定义为主函数中的函数变量(
var myCompletion=function(option){…}
),在这种情况下,它可以访问主函数的所有变量。否则,您希望在异步事件(单击)后继续该函数,但这不会发生。好的,让我们试试:)Thxwell这并不容易,因为我在mainFct(my_array)中传递了一个JS数组{var x=(函数myOption(x){alert(x);return x;}();在单击之前发送警报“Undefined”