Re: Help with Objects



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
.



Relevant Pages

  • Re: Servlet showing image from local file
    ... > I want to build a servlet that loads an image from the local disk and ... This servlet must just show a image. ... Personally, I prefer to use a byte array, but that is because I usually ... you might want to give a little thought to using buffered streams. ...
    (comp.lang.java.help)
  • Re: problems doing form from menu
    ... Example array name is: MyArray ... "INIT" Loads when the form is created. ... this will help you also Rob. ...
    (microsoft.public.fox.vfp.forms)
  • Re: dependency of sizeof operator
    ... Assuming plain array access with no calculation of indices except an increment, each one takes one iload operation, I would guess. ... The conclusion is that at least about half of the integer operations are ultimately going into array index operations, and that most numerical data is being handled in floating point. ... You have no evidence that the bulk of the integer loads from the stack where loads which had anything to do with indexing, unless you are claiming that any load of a local variable is indexing. ...
    (comp.lang.c)
  • qw with strings containing spaces
    ... I need to populate a select multiple on a web page when it loads with a series ... and then pushing the to-be-determined values onto the array later on. ... function using the commas as the delimiter. ...
    (perl.beginners)
  • Re: Text file vs DB
    ... My app runs, loads the ... big table into an array (Would I benefit from using SQL and not ... benefit from using the SQL database or if I'd be better off sticking ... Reading a XML file may be fastest. ...
    (microsoft.public.dotnet.languages.vb)