Creating Objects Dynamically : Objects Object Oriented : Language Basics : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
JavaScript DHTML Home »  Language Basics   » [  Objects Object Oriented  ]   
 



Creating Objects Dynamically

Please note that some example is only working under IE or Firefox.


/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke 

ISBN: 067231763X
Publisher Sams CopyRight 2000

*/
<html>
<head>
  <title>Intranet Employee Database</title>
  <script type="text/javascript">
  <!--
   
    // Global variables
    var i = 0;
   
    // Create Array objects
    var empList = new Array();
   
    // Employee object constructor
    function employee(FirstName, LastName, HomePhone, Ext,  EmailAddress) {
      this.FirstName = FirstName;
      this.LastName = LastName;
      this.HomePhone = HomePhone;
      this.Ext = Ext;
      this.EmailAddress = EmailAddress;
      this.show = show;
    }
   
    function show() {      
      alert(this.FirstName + "/n" +
            this.LastName + "/n" +
            this.HomePhone + "/n" +
            this.Ext + "/n" this.EmailAddress);
      }
   
    function addEmployeeObject(FirstName, LastName, HomePhone, Ext,
                                EmailAddress) {
      empList[inew employee(FirstName, LastName, HomePhone, Ext,
                                EmailAddress);
    }
   
    function insertRecord() {
      FirstName = document.form1.FirstName.value;
      LastName = document.form1.LastName.value;
      HomePhone = document.form1.HomePhone.value;
      Ext = document.form1.Ext.value;
      EmailAddress = document.form1.EmailAddress.value;
      i++;
      addEmployeeObject(FirstName, LastName, HomePhone, Ext, EmailAddress);
    }
   
    function showAll() {
      objWindow = window.open("""""width=600,height=300");
      objWindow.document.write("<html><body>");
      objWindow.document.write("<h1>Object Description</h1>");
      objWindow.document.write("<p>");
      for (var q=1; q<empList.length; q++) {
        objWindow.document.write("<strong>" + empList[q].FirstName
                                    " " + empList[q].LastName + "</strong></p>");
        objWindow.document.write("<p>" + empList[q].HomePhone + "</p>");
        objWindow.document.write("<p>" + empList[q].Ext + "</p>");
        objWindow.document.write("<p>" + empList[q].EmailAddress + "</p>");
   
      objWindow.document.write("</body></html>");
      }
      objWindow.document.close()
    }
  //-->
  </script></head>
<body>
  <h1>Dyanamic Object Creator</h1>
  <p>Enter employee information in the form below and click the Add button.
  Press the Show All button to view a list of all employees you have
  entered.</p>
  <form name="form1">
    <pre>
      First Name:      
      <input type=text size=20 maxlength=256 name="FirstName">
    </pre>
    <pre>
      Last Name:
      <input type=text size=20 maxlength=256 name="LastName">
    </pre>
    <pre>
      Home Phone:
      <input type=text size=20 maxlength=256 name="HomePhone">
    </pre>
    <pre>
      Ext.:
      <input type=text size=20 maxlength=256 name="Ext">
    </pre>
    <pre>
      Email Address:
      <input type=text size=20 maxlength=256 name="EmailAddress">
   
    </pre>
    <pre>
      <input type="button" name="Add" value="Add" onClick="insertRecord()">
      <input type="button" name="ShowAll" value="Show All"
        onClick="showAll()">
   
    </pre>
  </form>
</body>
</html>

           
       
Related examples in the same category
1.  Object utility: create, parse and profile
2.  Define Object, use its instance
3.  Define and use object
4.   Creating an Object and Using Object Instance Properties and Methods
5.   Object-Oriented Planetary Data Presentation
6.  The Book Object Definition
7.  Complete Example of Using the employee, client, and project Objects
8.  Source Code for the showBook() Example
9.  Using the Book Object Constructor
10.  Object to array
11.  Utility class for JavaScript class definitionHas Download File








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.