P
Peter Cartwright
I'm trying to create an array of row objects containing 'source'
arrays, which in the actual script are node values. The problem is the
row objects all end up with the same values - that of the last
'source' array.
It's something to do with the loop in getSource() because if I replace
it with source= new Array(i,i) the script behaves correctly. But that
would be useless for my purposes. Any ideas what the problem could
be?
// row constructor
function rowObject(thesource)
{
this.field=thesource;
}
// return array
var source=new Array();
function getSource(i)
{
for(j=0;j<2;j++)
{
source[j]=i;
}
return source;
}
// make objects
row=new Array();
for(i=0;i<2;i++)
{
row=new rowObject(getSource(i))
alert(row[0].field[0])
}
arrays, which in the actual script are node values. The problem is the
row objects all end up with the same values - that of the last
'source' array.
It's something to do with the loop in getSource() because if I replace
it with source= new Array(i,i) the script behaves correctly. But that
would be useless for my purposes. Any ideas what the problem could
be?
// row constructor
function rowObject(thesource)
{
this.field=thesource;
}
// return array
var source=new Array();
function getSource(i)
{
for(j=0;j<2;j++)
{
source[j]=i;
}
return source;
}
// make objects
row=new Array();
for(i=0;i<2;i++)
{
row=new rowObject(getSource(i))
alert(row[0].field[0])
}