Here's some JavaScript that does not do what I would like it to do:
var x = new Object();
var y = new Object();
var j;
for (j=0; j<5; j++) {
x['f'+j] = function () { print(j); };
}
for (j=0; j<5; j++) {
var jj = 0+j;
y['f'+j] = function () { print(jj); };
}
x.f0(); // prints 5
y.f0(); // prints 4
I want object x to contain 5 functions, each of which prints a
different value. (Actually, I'm building an HTML form with a table
where any row can be removed by clicking a button in that row...) The
two approaches above do not work. I've tried some tricks with eval,
but they don't work either. Any idea on how to do this?
--
DLL
var x = new Object();
var y = new Object();
var j;
for (j=0; j<5; j++) {
x['f'+j] = function () { print(j); };
}
for (j=0; j<5; j++) {
var jj = 0+j;
y['f'+j] = function () { print(jj); };
}
x.f0(); // prints 5
y.f0(); // prints 4
I want object x to contain 5 functions, each of which prints a
different value. (Actually, I'm building an HTML form with a table
where any row can be removed by clicking a button in that row...) The
two approaches above do not work. I've tried some tricks with eval,
but they don't work either. Any idea on how to do this?
--
DLL
Comment