0

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);
            }
        }
7
  • 3
    What is up+i supposed to be? Commented May 16, 2011 at 17:27
  • up0, up1, up2, and so on Commented May 16, 2011 at 17:28
  • up+i = function() isn't valid Javascript, that's why you get syntax error Commented May 16, 2011 at 17:29
  • 1
    @Fel that isn't really going to work. Have you considered using an array instead? e.g.: up[i] = ... Commented May 16, 2011 at 17:29
  • 1
    Ah. You can't do up+i, but you can do window['up' + i]. Though I don't recommend it Commented May 16, 2011 at 17:30

7 Answers 7

3

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);
}
Sign up to request clarification or add additional context in comments.

10 Comments

This isn't right, see the second comment for what he was trying to do. He was trying to make 10 functions, not an array of 10 functions.
@Nick, when clarifications to questions are added after the fact it's much better to comment to notify people and then later down vote not immediately
@jared the comment that i posted to Nick also applies to your suggestion.
@Fel what did you intend for $t to reference? It's hard to help unless we know the intent of that value
if i use $t+i i get the error, but this. $t0, $t1, $t2 works well. Probably the best choice is separate pieces of code... thanks anyway.
|
1

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).

Comments

0

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?

1 Comment

He wants it to generate up1, up2, up3, up4, etc.
0

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.

Comments

0

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

Comments

0

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

Comments

0

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

2 Comments

ReferenceError: $t is not defined. But if i use $t1 directly the error go away.
Don't know it is your syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.