473,243 Members | 1,551 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,243 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 1403
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...

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.