JavaScript
  Home arrow JavaScript arrow Page 6 - An Object Lesson In JavaScript
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

An Object Lesson In JavaScript
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2001-04-02

    Table of Contents:
  • An Object Lesson In JavaScript
  • Object Lessons
  • Sumthing New
  • Alpha Radiation
  • Add()ing Some More
  • Turning Up The Heat
  • Room With A View
  • Construction Crew
  • A Hot Date

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    An Object Lesson In JavaScript - Turning Up The Heat
    (Page 6 of 9 )

    Here's another example, this one a Thermometer object which allows you to convert between different temperature scales.

    <script language="JavaScript">
    // constructor
    function Thermometer(degrees, scale)
    {
    // methods
    this.convertToCelsius = convertToCelsius;
    this.convertToFahrenheit = convertToFahrenheit;
    this.raiseTemp = raiseTemp;
    // action to take
    if (scale == "f" || scale == "F")
    {
    this.scale = scale;
    this.degreesF = degrees;
    this.degreesC = 0;
    this.convertToCelsius();
    }
    else
    {
    this.scale = scale;
    this.degreesF = 0;
    this.degreesC = degrees;
    this.convertToFahrenheit();
    }
    }
    // conversion functions
    function convertToCelsius()
    {
    this.degreesC = (5.0/9.0) * (this.degreesF - 32.0);
    }
    function convertToFahrenheit()
    {
    this.degreesF = ((9.0/5.0) * this.degreesC) + 32.0;
    }
    // method to raise temperature
    function raiseTemp(num)
    {
    this.degreesF += num;
    this.degreesC += num;
    }
    </script>
    

    Nothing too fancy here - the constructor simply creates an object, initializes it with a temperature and temperature scale, and runs a conversion function to obtain the equivalent temperature in the other scale. A raiseTemp() method is included to demonstrate how object properties can be altered.

    It should be noted here that it is also possible to directly adjust the object properties without using the raiseTemp() method. I say "technically", because it is generally not advisable to do this, as it would violate the integrity of the object; the preferred method is always to use the methods exposed by the object to change object properties. By limiting yourself to exposed methods, you are provided with a level of protection which ensures that changes in the object constructor code do not have repercussions on your code.

    And here's how you could use the object in an HTML document.
    <script language="JavaScript">
    // create an object instance
    a = new Thermometer(98.6, "f");
    // access object properties
    alert("Temperature in Fahrenheit is " + a.degreesF);
    alert("Temperature in Celsius is " + a.degreesC);
    // execute object methods
    a.raiseTemp(10);
    alert("Temperature in Fahrenheit is " + a.degreesF);
    alert("Temperature in Celsius is " + a.degreesC);
    </script>
    

    Here's the result:

    More JavaScript Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway