M
mshiltonj
I've narrowed an issue down to a particular test case. I'm dynamically
adding functions to objects in a loop and trying embed certain data in
the function. The problem is that the function seems to apply the data
that existed in the last iteration of the loop to all previous
iterations. I've narrowed down an example to less than fifty lines.
The code assumes you have the FireBug extension running on FireFox...
<html>
<head> <title></title>
<script language="javascript">
var ary = new Array('a', 'b', 'c');
var objs = new Array();
var o1 = new Object();
o1.name = 'foo';
objs.push(o1);
var o2 = new Object();
o2.name = 'bar';
objs.push(o2);
var o3 = new Object();
o3.name = 'baz';
objs.push(o3);
for (idx in objs)
{
obj = objs[idx];
console.debug(obj.name);
console.debug(ary[idx]);
var func = function(name){
console.debug(name);
console.debug(ary[idx]);
console.debug(obj.name);
}
obj.func = func;
}
console.debug('-----');
o1.func(o1.name);
console.debug('-----');
o2.func(o2.name);
console.debug('-----');
o3.func(o3.name);
</script>
</head>
<body>
</body>
</html>
And the output to the firebug console is:
foo
a
bar
b
baz
c
----
foo
c
baz
-----
bar
c
baz
----
baz
c
baz
The second and third lines of each block between the line (except the
first block) is:
c
baz
But I was expecting the word ('baz') to match the first work in the
block. (i.e. 'foo')
I'm doing something wrong, or have a different expectation of the way
anonymous functions work in javascript. Can someone tell me where I'm
doing the wrong thing, and what is the proper way to achieve this
affect -- creating dymamic functions with varied data while iteration
over a loop? In my non-test code, I am unable to pass in variables to
the function call.
Thanks for any input.
adding functions to objects in a loop and trying embed certain data in
the function. The problem is that the function seems to apply the data
that existed in the last iteration of the loop to all previous
iterations. I've narrowed down an example to less than fifty lines.
The code assumes you have the FireBug extension running on FireFox...
<html>
<head> <title></title>
<script language="javascript">
var ary = new Array('a', 'b', 'c');
var objs = new Array();
var o1 = new Object();
o1.name = 'foo';
objs.push(o1);
var o2 = new Object();
o2.name = 'bar';
objs.push(o2);
var o3 = new Object();
o3.name = 'baz';
objs.push(o3);
for (idx in objs)
{
obj = objs[idx];
console.debug(obj.name);
console.debug(ary[idx]);
var func = function(name){
console.debug(name);
console.debug(ary[idx]);
console.debug(obj.name);
}
obj.func = func;
}
console.debug('-----');
o1.func(o1.name);
console.debug('-----');
o2.func(o2.name);
console.debug('-----');
o3.func(o3.name);
</script>
</head>
<body>
</body>
</html>
And the output to the firebug console is:
foo
a
bar
b
baz
c
----
foo
c
baz
-----
bar
c
baz
----
baz
c
baz
The second and third lines of each block between the line (except the
first block) is:
c
baz
But I was expecting the word ('baz') to match the first work in the
block. (i.e. 'foo')
I'm doing something wrong, or have a different expectation of the way
anonymous functions work in javascript. Can someone tell me where I'm
doing the wrong thing, and what is the proper way to achieve this
affect -- creating dymamic functions with varied data while iteration
over a loop? In my non-test code, I am unable to pass in variables to
the function call.
Thanks for any input.