JavaScript
  Home arrow JavaScript arrow Page 3 - Object-Oriented JavaScript: An Introductio...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Object-Oriented JavaScript: An Introduction to Core Concepts
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    2005-11-28

    Table of Contents:
  • Object-Oriented JavaScript: An Introduction to Core Concepts
  • Working with objects in JavaScript : introducing the key concepts
  • Coupling methods to objects: defining self-contained constructors
  • Functions and objects: a closer look at the “Function” object
  • Getting information about objects: looking at the “constructor” property

  • 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


    Object-Oriented JavaScript: An Introduction to Core Concepts - Coupling methods to objects: defining self-contained constructors


    (Page 3 of 5 )

    As the term suggests, self-contained constructors are simply regular constructors that contain, or include inside their structure, the definition for all the methods of the pertinent object. Returning to the example you saw earlier, the constructor for “Div” objects might be rewritten as follows:

    function Div(w,h,t,l,p,bg){
        // define object properties
        this.div=document.createElement('div');
        this.w=w+'px';
        this.h=h+'px';
        this.pos='absolute';
        this.top=t+'px';
        this.left=l+'px';
        this.pad=p+'px';
        this.bord='1px solid #000';
        this.bg='#'+bg;
        // define 'display()' method
        this.display=function(){
            this.div.style.width=this.w;
            this.div.style.height=this.h;
            this.div.style.position=this.pos;
            this.div.style.top=this.top;
            this.div.style.left=this.left;
            this.div.style.padding=this.pad;
            this.div.style.border=this.bord;
            this.div.style.background=this.bg;
            document.getElementsByTagName('body')[0].appendChild(this.div);
        }
    }

    Now, the constructor shows a slightly more compact structure, since all the properties, along with the definition for the “display()” method, are placed inside of its context. As you can appreciate, the above used syntax makes it easier to see at first glance what methods are exposed by the corresponding object. Of course, this notation doesn’t imply changing the way that objects are instantiated, therefore, if I wanted to spawn another “Div” object, I’d use the same syntax that you previously learned:

    var div=new Div(300,250,100,100,5,'00f');
    div.display();

    As shown above, spawning user-defined objects in JavaScript isn’t very different from other programming languages. Once the constructor has been defined, an object can be instantiated with the “new” keyword and assigned to a regular JavaScript variable.

    Now that you know how a self-contained constructor looks and works, it’s time to take a look at another interesting topic of object-based JavaScript: the “Function” object. Just scroll down the page and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This firt tutorial introduces the core concepts for creating user-defined objects in...
       · I was suprissed to see that the wrong way is still used. The correct way to OO in...
       · Thank you for commenting on my JavaScript article.Actually, the method for...
       · Hi, I've been using objects in JavaScript for quite a long time and it was always...
       · Thank you so much for the positive comments, since they're very useful....
       · Maybe it's because it's IE, but when I copied and pasted the code:function...
       · Hello,Thank you for your comments on this article. Regarding your question, I've...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek