JavaScript
  Home arrow JavaScript arrow Page 2 - Object-Oriented JavaScript: Building Real-...
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: Building Real-World Examples
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 76
    2005-12-12

    Table of Contents:
  • Object-Oriented JavaScript: Building Real-World Examples
  • The first hands-on example: building object-based pop-up windows
  • Object-oriented AJAX: creating http requester objects
  • A final example: building quick and dirty form validating objects

  • 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: Building Real-World Examples - The first hands-on example: building object-based pop-up windows


    (Page 2 of 4 )

    The first practical example that uses most of the concepts related to custom objects in JavaScript consists of developing a constructor function that returns pop-up window objects. Although overall, pop-ups can be considered rather outdated elements in modern Web development, in this particular case I’ll write the appropriate constructor, so you can see how the theory explained in the earlier articles can be applied to real-world situations.

    To begin, here’s the definition for the “popUpWin()” constructor function, along with a couple of additional object methods:

    // define popUpWin object
    function popUpWin(url,width,height,res,stat,loc,sbars,top,left){
        this.url=url;
        this.width=!width?200:width;
        this.height=!height?250:height;
        this.res=!res?false:true;
        this.stat=!stat?false:true;
        this.loc=!loc?false:true;
        this.sbars=!sbars?false:true;
        this.top=!top?0:top;
        this.left=!left?0:left;
        this.display=display;
        this.minimize=minimize;
        // display window
        function display(){
           this.win=window.open(this.url,'','width='+this.width+',height='+
    this.height+',resizable='+this.res+'status='+this.stat+',
    scrollbars='+this.sbars+'top='+this.top+',left='+this.left);
        }
        // minimize window
        function minimize(){
            this.win.blur();
        }
    }

    In simple terms, the above constructor function builds up some configurable pop-up windows, and as one would expect, accepts a series of parameters for using the window object itself and its “open()” method. Probably you’ve been working with similar functions in the past, so this one should be quite familiar to you.

    However, as you can see, working with pop-up constructor objects implies having some additional advantages that certainly aren’t present when using conventional functions. The main benefit rests on the ease of programmatically controlling the behavior of each pop-up window object, since each method can be called up according to the logic that a given application dictates. Take a look at the simple “minimize()” method, which exemplifies the high level of programmatic control exposed by each pertinent object:

    function minimize(){
        this.win.blur();
    }

    Now, after defining the corresponding constructor functions, as well as the additional “minimize()” method, it’s time to spawn a sample pop-up window object, and utilize its methods. The corresponding script could be written as follows:

    // instantiate popUpWin object
    var pWin=new popUpWin('',500,250);
    // open pop up window
    pWin.display();
    // minimize pop up window
    pWin.minimize();

    As the above example clearly shows, a new pop-up window object has been instantiated from its corresponding constructor, passing into it only the width and height of the window to be displayed. The remaining parameters take default values, so they’re not specified when the object is created.

    The rest of the snippet is responsible, first, for displaying the pop-up window, and second, for minimizing it through its homonymous method, which, as you can see, is simply a wrapper for the “blur()” window’s object method.

    After demonstrating how pop-up window objects can be created and properly utilized in JavaScript applications, the next example will rest on building http requester objects, something that you’ll hopefully find quite useful, particularly if you’re using AJAX very often in your JavaScript applications. So, let’s see how these objects can be defined and put to work for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This last tutorial shows some examples of how to create user-defined objects in...
       · [quote]The first improvement that comes to my mind is the addition of a “host”...
       · Hello Daniel,Thank you for commenting on my article. And, yes, you're correct on...
     

    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 5 hosted by Hostway
    Stay green...Green IT