Help with Objects

S

stuartshay

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.

Thanks
SPS

function onload()
{
var id = 25;
var previewProperties = new PreviewProperties();
previewProperties = tripPreviewProperties(id);

//Need to Display lat,lon & scale
//alert(previewProperties.lat); This Does Not Work

}

//Preview Properties Object
function PreviewProperties(lat,lon) {
this.lat = lat;
this.lon = lon;
this.scale =4;
}


function tripPreviewProperties(id)
{

AjaxCommon.GoogleMapsTripPreviewProperties(id,tripPreviewProperties_callback);
}


function tripPreviewProperties_callback(res)
{
properties = new PreviewProperties(res.value.Lat,res.value.Lon);

// Corect Properties are Displayed
alert(properties.lat + "\n" + properties.lon );

return properties;
}
 
R

RobG

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.
 
R

RobG

RobG wrote:
[...]
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 ) {

Sorry, that was not meant to be there (it's a pretty silly test)

var wp = el[el.selectedIndex].value;
var ref;
if ( '' != wp && (ref = wayPoint[wp]) ) {


and the first option should have no value:

<select onchange="showWaypoint(this);">
<option>Select a city</option>
...


[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top