SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 5 - 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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 / 19
    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
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Object-Oriented JavaScript: An Introduction to Core Concepts - Getting information about objects: looking at the “constructor” property


    (Page 5 of 5 )

    As I mentioned before, each time an object is instantiated, the function that defines the object itself is automatically called. In object-oriented parlance, this function is widely known as a “constructor”. In particular, JavaScript exposes a built-in “constructor” property, so when used in conjunction with a given object, it returns the complete definition for its constructor method. Sounds confusing? It’s not. Please study the example listed below to clarify any problem:

    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);
        }
    }

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

    As you can appreciate, the above example defines the same “Div” function I’ve used across this tutorial for spawning some DIV objects. According to the concepts previously explained, after instantiating a new “Div” object with the corresponding input parameters, it’s possible to use its “constructor” property, which comes in handy for returning the complete definition of the constructor method. The above “alert()” method returns the following output:

    As depicted above, the “constructor” property nicely returns the full definition for the object’s constructor, including its distinct properties, in conjunction with the list for each method. Give it a try using different objects, and watch the returning value of this property either when using built-in or user-defined objects. The experience is really instructive. Trust me.

    At this point, I’ve conveniently illustrated some of the core concepts of how to create and utilize user-defined objects in JavaScript. Of course, there are a number of topics worth reviewing yet, including the “prototype” property and how to apply Inheritance to multiple objects. But, as you may have guessed, these points will be properly addressed in the next tutorial.

    To wrap up

    Throughout this first article, I’ve explored in detail the basics for working with user-defined objects in JavaScript. Hopefully, this tutorial has provided you with the general guidelines that will allow you to start quickly spawning and utilizing your own objects in client-side applications. As I said previously, over the next part of the series, I’ll explain the correct implementation for the “prototype” property, as well as how to inherit properties from base objects. Sounds really interesting, right? So, don’t miss the next tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...

    Iron Speed

    Iron Speed





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