JavaScript
  Home arrow JavaScript arrow Page 3 - Creating User-Defined JavaScript Objects, ...
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

Creating User-Defined JavaScript Objects, Properties and Methods
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-10-09

    Table of Contents:
  • Creating User-Defined JavaScript Objects, Properties and Methods
  • The Top Level eval Function
  • User Created Objects
  • Creating User-Created 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


    Creating User-Defined JavaScript Objects, Properties and Methods - User Created Objects


    (Page 3 of 4 )

    As you can deduce from the previous page, the user can create objects and add properties and methods to the object. The user has to input the constructor function as a string argument to the eval() function, and that would create the object type. The user can then go on to add properties and methods as we did above. If the user wants to add a method, he has to first use the eval function to create the corresponding function, if the function does not already exist in the code.

    Let us now create an object type and object, add a property and a method to the object, and then display the added property and execute the added method, as a user would. We shall use the code segments we have already used before.

    Type the following and save as an HTML file.

    <html>

    <head>

    <script type="text/JavaScript">


    function userAdd()

    {

    var evalStr = prompt("Enter the two statements","");

    eval(evalStr);


    //verify if the property and method were added.

    alert(theObject.property1);

    theObject.method1();

    }


    </script>

    </head>

    <body>

    <button type="button" onclick="userAdd()">User Add</button>

    </body>

    </html>

    Open the above HTML file. Looking at the code as it is, when you click the "User Add" button, the statements that you type into the prompt box are enclosed in a string and then assigned to the evalStr variable. The eval() function then evaluates the statements. Next, it is verified whether the property and method were actually added.

    We shall put in the statements segment by segment. So until the last segment is input, the verification (last two statements in the userAdd() function) will not give the right result.

    More JavaScript Articles
    More By Chrysanthus Forcha


     

    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 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek