关于javascript循环的问题

发布于 2024-11-07 09:57:14 字数 205 浏览 0 评论 0原文

我尝试了这个循环,但不起作用(语法错误)。 正确的做法是什么?例如,up+i 不起作用。

  for(i=0;i<10;i++) {
            up+i = function() {
                base.refresh("Field"+i, $t+i);
            }
        }

I tried this loop, but isn't working (Syntax errors).
What is the correct way to do it? up+i doesn't work for example.

  for(i=0;i<10;i++) {
            up+i = function() {
                base.refresh("Field"+i, $t+i);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

焚却相思 2024-11-14 09:57:14

主要问题是围绕这一行

up+i = function() {

表达式 up+i 产生一个值,而不是一个变量,以及一个可以分配的位置。您是否尝试分配到数组中?如果是这样,请将其更改为以下

up[i] = function() {

编辑

OP,阐明其意图是创建 10 个命名函数。在这种情况下,需要有一个物体来悬挂它们。我将其称为 root 作为示例。

var root = {};
for (i = 0; i < 10; i++) {
  root['up' + i] = function() { 
    base.refresh("Field"+i, $t+i);
  };
}

此外,现在该函数正在捕获单个 i ,这意味着该函数的所有实例都将具有相同的 i 值。为了防止这种情况,请使用单独的函数来捕获该值。

var root = {};
for (i = 0; i < 10; i++) {
  root['up' + i] = function(arg) { 
    return function() { base.refresh("Field"+arg, $t+arg); };
  } (i);
}

The primary issue is around this line

up+i = function() {

The expression up+i produces a value, not a variable, and a place which can be assigned to. Were you trying instead to assign into an array? If so change it to the following

up[i] = function() {

EDIT

OP clarified that the intent is to create 10 named functions. In that case there needs to be an object to hang them off of. I'll call it root for an example

var root = {};
for (i = 0; i < 10; i++) {
  root['up' + i] = function() { 
    base.refresh("Field"+i, $t+i);
  };
}

Additionally right now the function is capturing a single i meaning all instances of the function will have the same value for i. To prevent this use a separate function to capture the value.

var root = {};
for (i = 0; i < 10; i++) {
  root['up' + i] = function(arg) { 
    return function() { base.refresh("Field"+arg, $t+arg); };
  } (i);
}
把时间冻结 2024-11-14 09:57:14

负责循环工作的代码。请看这里: http://jsfiddle.net/7aZLv/ (警告!依次打开 10 个警报框)

编辑:

您可以创建如下全局函数:http://jsfiddle.net/7aZLv/1/

for(i=0;i<10;i++) {
    window['up'+i] = function(){
        alert('test');
    }
}

up3(); // will make alert appear

发生错误是因为您的赋值表达式不正确(您将值赋给表达式,而不是变量应如何分配)。

The code responsible for looping works. Just see here: http://jsfiddle.net/7aZLv/ (warning! opens 10 alert boxes one after another)

EDIT:

You can create global functions like that: http://jsfiddle.net/7aZLv/1/

for(i=0;i<10;i++) {
    window['up'+i] = function(){
        alert('test');
    }
}

up3(); // will make alert appear

The error happened because you assignment expression was incorrect (you assigned value to expression, not a variable how it should be assigned).

一花一树开 2024-11-14 09:57:14

也许应该只是这样

for(i=0;i<10;i++) {
        up = function() {
            base.refresh("Field"+i, $t+i);
        }
    }

?我们需要大量信息来帮助您,应该做什么,您想要实现什么目标?您遇到什么语法错误?

Maybe it should just be

for(i=0;i<10;i++) {
        up = function() {
            base.refresh("Field"+i, $t+i);
        }
    }

? We need lots of information to help you, what is up supposed to be, what are you trying to accomplish? What syntax errors are you getting?

半暖夏伤 2024-11-14 09:57:14

赋值语句的左侧不能有表达式。 up+i 应该是指针算术吗?如果是这样,则 javascript 中没有指针。我不知道你想做什么,但首先要做的就是将 up+i = function() { ...} 更改为 up = function() {。 ..} 或者也获取一个新变量来分配它。

You cannot have an expression on the left hand side of an assignment. is up+i supposed to be pointer arithmetic? if so, there are no pointers in javascript. i cannot tell what you are trying to do, but the first thing to do would be to change up+i = function() { ...} to up = function() {...} or else get a new variable to assign it too.

淡水深流 2024-11-14 09:57:14

您的代码中有很多错误。

向上
是字符串?
$t
这是什么?

也许像这样?

for(i=0;i<10;i++) {
var new_func = 'up' + i;
        new_func = function() {
            base.refresh("Field"+i, $t+i);
        }
    }

Many errors in your code.

up
is string?
$t
what is this?

maybe like this?

for(i=0;i<10;i++) {
var new_func = 'up' + i;
        new_func = function() {
            base.refresh("Field"+i, $t+i);
        }
    }
☆獨立☆ 2024-11-14 09:57:14

您可以将它们添加到数组中,而不是尝试为函数创建单独的变量。

var ups;

for(i=0;i<10;i++) {
    ups.push(function() {
        base.refresh("Field"+i, $t+i);
    });
}

Instead of trying to create individual variables for the functions, you can add them to an array.

var ups;

for(i=0;i<10;i++) {
    ups.push(function() {
        base.refresh("Field"+i, $t+i);
    });
}
赏烟花じ飞满天 2024-11-14 09:57:14

尝试一下

for(i=0;i<10;i++) {
   window["up" +i] = function() {
      base.refresh("Field"+i, $t+i);
   }
}

这会将函数添加到 window 对象中,以便您可以在其之后进行如下调用:

up0();
up1();
// etc

try this

for(i=0;i<10;i++) {
   window["up" +i] = function() {
      base.refresh("Field"+i, $t+i);
   }
}

This will add the functions to the window object so that you can make calls like this after it:

up0();
up1();
// etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文