473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1429
Ia*****@gmail.c om 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.c om 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 addressObjToTex t(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(addressOb jToText(address Obj));
If addressObj is at all sparse (i.e. indexes are not contiguous from 0)
addressObjToTex t() will not show all the elements.

[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/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.c om 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
1994
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 + el2; if (value1 > value2) return 1;
4
14879
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the...
5
1141
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 form should be out of scope. I guess in a sence it is, because you can never "get ahold of it" again. However the form is still there. Maybe my...
2
3188
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. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately...
7
2172
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
3516
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 two part question. Part 1: I have a form with a text box on it that I want to spit out results to. Sample code:
26
2126
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); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
20
1883
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 global var private void method1(int myInt){ int x; //local var etc...
3
1765
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 CThread { CThread(){};
3
2461
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 (wxPython) Python program that needs to access external data across a serial port. The first thing that I need the program to do when it starts up is to...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.