...

View Full Version : Trying to make / use an array of objects



iuse
08-18-2007, 11:37 PM
This is what I got going on as far as declaring the class and the array:


function Toll(minX,minY,maxX,maxY)
{
this.minX=minX;
this.minY=minY;
this.maxX=maxX;
this.maxY=maxY;

this.tollSlope = ((minY - maxY)/(maxX-minX));

this.tollInter = minY - (tollSlope * maxX);
}

Toll.method('getMinX', function () {
return this.minX;
});

var myTolls = new Array()
myTolls[0] = new Toll(-81.20961, 28.45135, -81.20945, 28.45173);

and then later on in another function I try to call the getMinX function:


myTolls[0].getMinX

but when I run this in IE I get an error that says myTolls is not an object...
I am saying to myself of course myTolls is not an object. The value at element 0 in myTolls array is the object... what am i doing wrong?

(first time poster so if I am doing this wrong give me a heads up)

iuse
08-20-2007, 12:10 PM
I am still having this problem. Does anyone know another way that I could create a class so that I can make multiple objects with the same kind of properties and methods?
Does it seem what I am trying to do with the array and the class seem logical?
any ideas or suggestions would be great.

liorean
08-20-2007, 12:53 PM
From the code you've posted, I can see nothing that would go wrong in iew. One possible reason would be if the
var myTolls = new Array()piece is in another script body which contains a syntax error, or in an external script file that gets loaded after the
myTolls[0] = new Toll(-81.20961, 28.45135, -81.20945, 28.45173);piece.

Could you show us the code in action?



It's recommended that instead of using new Array() you should use []. It's smaller, stands out more, and is more consistent.

liorean
08-20-2007, 01:00 PM
Of course, this was obvious after running it...


function Toll(minX,minY,maxX,maxY)
{
this.minX=minX;
this.minY=minY;
this.maxX=maxX;
this.maxY=maxY;

this.tollSlope = ((minY - maxY)/(maxX-minX));

this.tollInter = minY - (tollSlope* maxX);
}
tollSlope --> this.tollSlope



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum