Re: Help with Objects
- From: RobG <rgqld@xxxxxxxxxxxxxx>
- Date: Wed, 03 Aug 2005 20:11:02 +1000
stuartshay@xxxxxxxxx wrote:
Hello All:
I am trying to figure out how to use objects properties in my code so when the page loads All my properties are in object where I can use elsewhere in my page.
I am new to JavaScrip Objects so any help would be appreciated.
[...]
The first part of any design is knowing the requirements. What are the objects for and what data will they hold?
For example, below is a simple object to hold way points and some code to present the values for each point.
<script type="text/javascript">
var wayPoint = {};
wayPoint.City_1 = { lat:150.0, lon:27.2, scale: 5 };
wayPoint.City_2 = { lat:151.2, lon:27.5, scale: 5 };
wayPoint.City_3 = { lat:153.4, lon:27.6, scale: 5 };function showWaypoint( el ){
var wp = el[el.selectedIndex].value;
var ref = wayPoint[ wp ];
if ( '' != ref ) {
alert( wp
+ '\nLatitude: ' + ref.lat
+ '\nLongitude: ' + ref.lon
+ '\nScale: ' + ref.scale
);
}
}
</script><select onchange="showWaypoint(this);"> <option value="City_1">City 1</option> <option value="City_2">City 2</option> <option value="City_3">City 3</option> </select>
or done slightly differently:
var wayPoint = {
City_1 :{ lat:150.0, lon:27.2, scale: 5 },
City_2 :{ lat:151.2, lon:27.5, scale: 5 },
City_3 :{ lat:153.4, lon:27.6, scale: 5 }
};Or you could give wayPoints some methods to add way points and then feed it data from an array. But it seems pointless to create an array just to feed to an object when you could load the object with the data in the first place and not bother with the method.
But if you want to modify the object's data it may be better if the methods belong to the object.
As I said, it all depends on what you want this thing to do.
-- Rob .
- Follow-Ups:
- Re: Help with Objects
- From: RobG
- Re: Help with Objects
- References:
- Help with Objects
- From: stuartshay
- Help with Objects
- Prev by Date: Re: Hidden tables and form
- Next by Date: Re: Help with Objects
- Previous by thread: Help with Objects
- Next by thread: Re: Help with Objects
- Index(es):
Relevant Pages
|