Javascript 我的代码有什么问题?它应该输出0-9,但没有输出。我认为错误在go()中
你能帮我调试这个代码吗?我看不出其中的错误。也没有用于确定错误的输出 函数go() { var程序=[]; 对于(变量i=0;i<10;i++) { 过程[过程.长度]=函数() { 警惕(“你现在“+i+”岁”); } 运行程序; } 函数运行程序(procs) { 对于(变量i=0;iJavascript 我的代码有什么问题?它应该输出0-9,但没有输出。我认为错误在go()中,javascript,Javascript,你能帮我调试这个代码吗?我看不出其中的错误。也没有用于确定错误的输出 函数go() { var程序=[]; 对于(变量i=0;i
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;i go();// 为了方便起见,我已经对您的一些问题进行了评论
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();
}