Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的代码有什么问题?它应该输出0-9,但没有输出。我认为错误在go()中_Javascript - Fatal编程技术网

Javascript 我的代码有什么问题?它应该输出0-9,但没有输出。我认为错误在go()中

Javascript 我的代码有什么问题?它应该输出0-9,但没有输出。我认为错误在go()中,javascript,Javascript,你能帮我调试这个代码吗?我看不出其中的错误。也没有用于确定错误的输出 函数go() { var程序=[]; 对于(变量i=0;i

你能帮我调试这个代码吗?我看不出其中的错误。也没有用于确定错误的输出

函数go() { var程序=[]; 对于(变量i=0;i<10;i++) { 过程[过程.长度]=函数() { 警惕(“你现在“+i+”岁”); } 运行程序; } 函数运行程序(procs) { 对于(变量i=0;i
  • 在阵列中反复加载同一点
  • 你叫procs?什么是过程
  • 进去就是进去
  • 请尝试以下修复程序:

    var procedures = [];
    function go()
    {
       for (var i = 0; i < 10; i++) 
       {
           procedures[i] = function () 
           {
               alert("You are now " + i + " years old");
           }
           run_procs();
       }
    
    
    }
    
    function run_procs(procs)
    {
        for (var i = 0; i < procedures.length; i++)
         {
             procedures[i]();
         }
     }
    
     go();
    
    var过程=[];
    函数go()
    {
    对于(变量i=0;i<10;i++)
    {
    程序[i]=函数()
    {
    警惕(“你现在“+i+”岁”);
    }
    运行程序();
    }
    }
    函数运行程序(procs)
    {
    对于(var i=0;i

    jsidle示例

    您已经在函数内部执行了函数。Put
    go()外部。

    您正在调用函数
    go
    内部
    go
    ,以创建不确定递归。您需要从函数外部调用函数
    go
    ,以开始执行函数

    function go()
    {
        var procedures = [];
    
        for (var i = 0; i < 10; i++) 
        {
            procedures[procedures.length] = function () 
            {
                console.log("You are now " + i + " years old");
            } 
            run_procs(procedures);
        }
    
        function run_procs(procs) 
        {
            for (var i = 0; i < procs.length; i++)
            {
                procs[i]();
            }
        }
    
    }
    
    go(); // <<-- this should be outside the function body
    
    函数go() { var程序=[]; 对于(变量i=0;i<10;i++) { 过程[过程.长度]=函数() { log(“您现在”+i+“岁”); } 运行程序; } 函数运行程序(procs) { 对于(变量i=0;igo();// 为了方便起见,我已经对您的一些问题进行了评论

     function go()
     {
     var procedures = [];
    
     for (var i = 0; i < 10; i++) 
     {
         procedures[procedures.length] = function () 
         {
             alert("You are now " + i + " years old");
             //this function doesn't return anything, so you will never assign any values to procedures[procedures.length]
         } 
         run_procs(procedures);
     }
    
     function run_procs(procs) 
     {
         for (var i = 0; i < procs.length; i++)
         {
             procs[i]();
             //the argument you have passed to this function is not a function itself, so this won't do much.
         }
     }
     //this is inside the go() declaration so you aren't actually calling it. 
     go();
     }
    
    函数go() { var程序=[]; 对于(变量i=0;i<10;i++) { 过程[过程.长度]=函数() { 警惕(“你现在“+i+”岁”); //此函数不返回任何内容,因此您永远不会向过程[procedures.length]分配任何值 } 运行程序; } 函数运行程序(procs) { 对于(变量i=0;i

    这些代码都不会运行,因为您只是声明了函数,而没有实际调用它。因此,您没有收到任何错误。

    您收到错误了吗?如果是,错误是什么?
     function go()
     {
     var procedures = [];
    
     for (var i = 0; i < 10; i++) 
     {
         procedures[procedures.length] = function () 
         {
             alert("You are now " + i + " years old");
             //this function doesn't return anything, so you will never assign any values to procedures[procedures.length]
         } 
         run_procs(procedures);
     }
    
     function run_procs(procs) 
     {
         for (var i = 0; i < procs.length; i++)
         {
             procs[i]();
             //the argument you have passed to this function is not a function itself, so this won't do much.
         }
     }
     //this is inside the go() declaration so you aren't actually calling it. 
     go();
     }