Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the length of an Array? 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
this is my code:

function MyThing(va, vb)
{
this.va = va
this.vb = vb
}

var NewThing = new MyThing(4)
NewThing[0].va = "varA"
NewThing[0].vb = "VarB"
NewThing[1].va = "varA"
NewThing[1].vb = "VarB"
.....

How do I do NewThing.Length?

I would think it would be 4? But it's null







The door to life is never locked, but few have the knowledge to open it.
 
ohh... nevermind... you cant get the length of your object... it doesn't have a length property...

you object isn't an array... you can put your objects in an array though...

var x = new Array()
x[x.length] = new MyThing(); adam@aauser.com
 
I tried this:
function mything(a,b)
{
this.a = a
this.b = b
}

thething = new Array(3)

thething[0] = new mything("aaa","bbb")

thething[1] = new mything()
thething[1].a = "1"
thething[1].b = "2"

document.writeln(thething[0].a + &quot;<br>&quot;);
document.writeln(thething[0].b + &quot;<br>&quot;);
document.writeln(thething[1].a + &quot;<br>&quot;);
document.writeln(thething[1].b + &quot;<br>&quot;);
document.writeln(thething.length + &quot;<br>&quot;);

Is there anything wrong with doing it this way?
The door to life is never locked, but few have the knowledge to open it.
 
Yes, the last code worked, but I'm asking if there's anything wrong with doing it this way?

I'm new to JavaScript objects. In Visual Basic you need to set them to nothing and do more coding to keep from getting memory leaks.
JS arrays are too easy. I have to forget all I learned in VB :) The door to life is never locked, but few have the knowledge to open it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top