473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Scope and object question

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

Apr 6 '06 #1
4 1405
Ia*****@gmail.com wrote:
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
Apr 6 '06 #2
Ia*****@gmail.com 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:

<URL:http://www.json.org/>

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[i];
txt[i] = [];

for (a in addr){
txt[i].push(addr[a]);
}
txt[i] = txt[i].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.

[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 7 '06 #3
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.

Apr 9 '06 #4
VK

Ia*****@gmail.com wrote:
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) ?

Apr 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
5
by: Samud | last post by:
Hi, I've got a question about something which actually works, but I'd like to know why... If you create a form in a subroutine and "show" it, once the subroutine finishes, technically the new...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
3
by: Pantokrator | last post by:
Hi all, I've got a question about the scope of the constructor initialisation list. First of all, let me explain my classes: // ***************************************************** class...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.