JavaScript
  Home arrow JavaScript arrow Page 4 - 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 
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 / 20
    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 - Functions and objects: a closer look at the “Function” object


    (Page 4 of 5 )

    A less-known concept in JavaScript is that each function is itself an object. The reason for this rests on the automatic creation of a “Function” object each time a regular function is defined, without specifying the “new” keyword, as one would expect to do with conventional objects. To understand this concept, have a look at the example below, which creates a “Div” function, and then adds some properties to the “Function” object:

    function Div(){}
        Div.w='400px';
        Div.h='300px';
        Div.pos='absolute';
        Div.top='20px';
        Div.left='20px';
        Div.pad='5px';
        Div.bord='1px solid #000';
        Div.bg='#ff0';
        // define 'display()' method
        Div.display=function(){
     var div=document.createElement('div');
     div.style.width=Div.w;
     div.style.height=Div.h;
     div.style.position=Div.pos;
     div.style.top=Div.top;
     div.style.left=Div.left;
     div.style.padding=Div.pad;
     div.style.border=Div.bord;
     div.style.background=Div.bg;
     document.getElementsByTagName('body')[0].appendChild(div);
        }

    In the example above, I’ve defined a “Div()” function, which automatically has created a “Function” object. Provided that an object has some properties and methods, what I’ve done is simply assign a few properties to this “Div” object, in addition to the “display()” method. Finally, the method can be called up as you’d do normally with a regular object, as following:

    Div.display();

    Even when the “Function” object actually isn’t very popular, and certainly isn’t used very frequently in JavaScript applications, the above example demonstrates the basics of how to use it appropriately in your scripts.

    After having explained some of the key points regarding the unusual “Function” object, let’s move on and continue learning more about user-defined objects in JavaScript. In this case we will take a look at the “constructor” property. Want to know more? Right, let’s explore this topic together.

    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 Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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