Scope and object question

I

IanONet

I had a need for a two dimentional array. Looking for this solution, I
ran accross a statement than all Javascipt arrays were arrays of
objects. So I created a function prototype, at least thats what I was
calling it:

function objRow(vartype, varaddr1, varaddr2)
{
this.type = vartype;
this.addr1 =varaddr1;
this.addr2 =varaddr2;
}

Next I did:
var myobject=new objRow("1", "1234 Main St.", "Apt 101");

At this point I was able to see myobject.addr1 or any other variable in
the object instance.

Now I added this object to a table.
var aryTestTable= new Array();
aryTestTable[0]= myobject;
At this point I could see
aryTestTable[0].addr1
Next I tried an additional object
myobject=new objRow("1", "1234 Main St.", "Apt 101"); //with
different data
And added it to the table
aryTestTable[1]= myobject;
Where I could see:
aryTestTable[1].addr2 or any other variable.

so far so good. Then I started the actual application code where I was
reading a database table and creating the objects and adding them to
the table. This was in a for loop wherein the myobject=new objRow("1",
"1234 Main St.", "Apt 101"); was instantiated.

After the for loop was finished, I could not access the data in the
table - undefined.
So my questions are: Have the my object instances popped off the stack?
and What is the alternative way to implement this table of rows of
values.
Thanks in advance,
IanO
 
T

Thomas 'PointedEars' Lahn

I had a need for a two dimentional array. Looking for this solution,
I ran accross a statement than all Javascipt arrays were arrays of
objects.

Either the person writing it had no clue what (s)he was talking about,
or you misunderstood him/her completely.

True is that there are no built-in multi-dimensional arrays in ECMAScript
implementations (and other programming languages). To create something
which elements that can be addressed this way, one has to create an Array
object encapsulating an array data structure which elements are references
to (other) Array objects.
So I created a function prototype, at least thats what I was
calling it:

function objRow(vartype, varaddr1, varaddr2)
{
this.type = vartype;
this.addr1 =varaddr1;
this.addr2 =varaddr2;
}

Next I did:
var myobject=new objRow("1", "1234 Main St.", "Apt 101");

At this point I was able to see myobject.addr1 or any other variable in
the object instance.

Now I added this object to a table.
var aryTestTable= new Array();
aryTestTable[0]= myobject;
At this point I could see
aryTestTable[0].addr1
Next I tried an additional object
myobject=new objRow("1", "1234 Main St.", "Apt 101"); //with
different data
And added it to the table
aryTestTable[1]= myobject;
Where I could see:
aryTestTable[1].addr2 or any other variable.

That works, but it is unnecessarily complicated, and error-prone
(`myobject' has not been declared, ). Try this instead:

function Row(...)
{
...
}

var aTestTable = new Array(
new Row("1", "1234 Main St.", "Apt 101"),
...
);
so far so good. Then I started the actual application code where I was
reading a database table and creating the objects and adding them to
the table. This was in a for loop wherein the myobject=new objRow("1",
"1234 Main St.", "Apt 101"); was instantiated.

After the for loop was finished, I could not access the data in the
table - undefined.

Could it possibly be that the document resource changed after you created
the Array object? If so, there would be no more reference to either
object. (See below)
So my questions are: Have the my object instances popped off the stack?

You are not making any sense. Object instances? Stack?

1. The language you are using implements prototype-based inheritance.
Objects inherit from other objects, not from classes. Hence the
term object instance is misleading, at best.

2. Objects are stored in memory; like all dynamic data, they are stored
on the heap, where memory is allocated to store that data, and freed
(by the Garbage Collector) when it is no longer needed (i.e. there is
no reference to the object anymore).

3. The stack stores only references to _local_ _variables_.
and What is the alternative way to implement this table of rows of
values.

Your approach is one of the better ones, but apparently you have not
understood yet what you are doing. Hence the result you observed.


PointedEars
 
R

RobG

(e-mail address removed) said on 07/04/2006 7:18 AM AEST:
I had a need for a two dimentional array. Looking for this solution, I
ran accross a statement than all Javascipt arrays were arrays of
objects.

JavaScript arrays are objects with few built-in methods (pop, join,
push, etc.) and a special length property. They have a few other
special features as well.

So I created a function prototype, at least thats what I was
calling it:

function objRow(vartype, varaddr1, varaddr2)
{
this.type = vartype;
this.addr1 =varaddr1;
this.addr2 =varaddr2;
}

Next I did:
var myobject=new objRow("1", "1234 Main St.", "Apt 101");

At this point I was able to see myobject.addr1 or any other variable in
the object instance.

Now I added this object to a table.
var aryTestTable= new Array();
aryTestTable[0]= myobject;
At this point I could see
aryTestTable[0].addr1

So - aryTestTable[0] - is a reference to the object - myobject -. I
think Thomas has suggested a more efficient method. You could also
create the structure as an object literal:

var myobject = [
{vartype : '1',
varaddr1 : '1234 Main St.',
varaddr2 : 'Apt 101'},
{vartype : '1',
varaddr1 : '5678 West St.',
varaddr2 : 'Apt 6'}
];

Which will lead you to JSON:

Next I tried an additional object
myobject=new objRow("1", "1234 Main St.", "Apt 101"); //with
different data
And added it to the table
aryTestTable[1]= myobject;
Where I could see:
aryTestTable[1].addr2 or any other variable.

so far so good.
Yup.


Then I started the actual application code where I was
reading a database table and creating the objects and adding them to
the table. This was in a for loop wherein the myobject=new objRow("1",
"1234 Main St.", "Apt 101"); was instantiated.

After the for loop was finished, I could not access the data in the
table - undefined.

You need to show how the loop is coded. If myobject is a local variable
and you exit the function without returning a reference to it to some
other object/function, there is a good chance it doesn't exist any more.

Try this sample:

var addressObj = [
{vartype :'1', varaddr1 : '1234 Main St.', varaddr2 : 'Apt 101'},
{vartype :'1', varaddr1 : '5678 West St.', varaddr2 : 'Apt 6'}
];

function addressObjToText(obj)
{
var a, addr, txt = [];
var i = 0;

do {
addr = addressObj;
txt = [];

for (a in addr){
txt.push(addr[a]);
}
txt = txt.join(', ');

} while (addressObj[i++])

return txt.join('\n');
}

alert(addressObjToText(addressObj));


If addressObj is at all sparse (i.e. indexes are not contiguous from 0)
addressObjToText() will not show all the elements.

[...]
 
I

IanONet

Thanks for all your replies. I appologise for confusion I caused by
misusing the word prototype. The concept that I implemented is a
constructor function.

The answer to the original question is to use String( arround the field
being passed to the constructor function). I hope this helps the next
person whos has this issue.
 
V

VK

The answer to the original question is to use String( arround the field
being passed to the constructor function). I hope this helps the next
person whos has this issue.

That is absolutely doubtless positively impossible: unless you did some
really weird augmentation with the native String object. The strings
got through the explicit String() constructor are using separate
constructor, not the same as for string literals. That is the only
difference:- and the only little possibility to make your assumption
right.

No offence, but I give much better chance to the "minus and minus gets
plus" situation which happens sometimes. Few days ago I was fixing a
custom made site with a very nice layout. Inside it was a holly mess
nested tables where a half of cells were declared as <tdnobr> (the
author did not know that you have to place a space: <td nobr>). The
funny thing is that the layout was totally cool as long as one did not
try to fix it: because <tdnobr>'s were occasionally compensated by a
set of other mistakes here and there.

Again - no offence, but I feel that your code right now is in that
exact state. You may leave it as it is of course, but any new minus
will put your "summary sign" back to minus (remember the math? ;-)

Maybe it is better to look at the actual code (in full) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top