JavaScript
  Home arrow JavaScript arrow Page 5 - 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    An Object Lesson In JavaScript - Add()ing Some More
    (Page 5 of 9 )

    Just as you can define object properties, it's also possible to define object methods - essentially, simple JavaScript functions. A little more evolution, and the Sumthing object now sports an Add() method, which adds the values stored in "alpha" and "beta".
    <script language="JavaScript">
    // object constructor
    function Sumthing(num1, num2) {
    // object properties
    this.alpha = num1;
    this.beta = num2;
    // object methods
    this.Add = Add;
    }
    // object method Add() - add arguments
    function Add() {
    sum = this.alpha + this.beta;
    return sum;
    }
    </script>
    

    A couple of interesting things here. First, the object method Add() is actually defined outside the object constructor block, though it references object properties using the "this" keyword. And second, just as object properties are defined using "this", object methods need to be defined in the same manner - witness my addition of
    function Sumthing(num1, num2) {
    ...
    // object methods
    this.Add = Add;
    ...
    }
    

    to the object constructor block.

    Wanna see how it works? Take a look at the code
    <script language="JavaScript">
    obj = new Sumthing(2, 89);
    alert("The sum of " + obj.alpha + " and " + obj.beta + " is " + obj.Add());
    </script>
    

    and the output.



    And now that your object constructor is all ready to go, let's see a quick demonstration of how you can use it to spawn multiple instances of the same object, each operating independently of the other.
    <script language="JavaScript">
    // one object
    obj1 = new Sumthing(2, 89);
    alert("The sum of " + obj1.alpha + " and " + obj1.beta + " is " +
    obj1.Add());
    // another one
    obj2 = new Sumthing(546, 67);
    alert("The sum of " + obj2.alpha + " and " + obj2.beta + " is " +
    obj2.Add());
    // and a third one
    obj3 = new Sumthing(2364237, 283457);
    alert("The sum of " + obj3.alpha + " and " + obj3.beta + " is " +
    obj3.Add());
    </script>
    

    Now *that* is cool!

    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

    Iron Speed
     
    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 3 hosted by Hostway