SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Tennessee
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating a constructor - instantiate - functions

    Please someone help! I have been working on this code for days and I cannot get it to work. I am so confused!!

    Here is the code:

    <HTML>
    <HEAD>
    <TITLE>Employee Database</TITLE>

    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    <!--

    function employeeObject(name,department,extension) {

    this.name = name;
    this.department = department;
    this.extension = extension;
    }



    var employees = new Array();
    employees[1] = new productObject("Mai Li", "Sales", 551);
    employees[2] = new productObject("Maria Alvarez", "Human Resources", 441);
    employees[3] = new productObject("Tom Smith", "Marketing", 331);

    len = employees.length;

    function showEmployee() {
    var info = "";




    alert(info);
    }

    function showAllEmployees() {
    var info = "";
    for (var i = 1; i < len; i++) {
    info += "Employee: " + employees[i].name + "\n";
    info += "Department: " + employees[i].department + "\n";
    info += "Extension: " + employees[i].extension + "\n\n";
    }

    alert(info);
    }

    //-->
    </SCRIPT>

    </HEAD>
    <BODY>

    <H3>Employee Database</H3>
    <HR>
    <FORM NAME="empForm">
    <B>Select name to view information/B>
    <SELECT NAME="empName" onChange="employees[this.selectedIndex].showEmployee();this.selectedIndex=0;">

    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    <!--
    for (var i = 0; i < len; i++) {
    if(i == 0) document.write("<OPTION>" + employees[i]);
    else document.write("<OPTION>" + employees[i].name);
    }

    //-->
    </SCRIPT>

    </SELECT>
    <P>
    <INPUT TYPE="button" VALUE="show all employees" onClick="showAllEmployees();">

    </FORM>

    </BODY>
    </HTML>

    Ok, here is what I need to do. I need to finish the employeeObject constructor, adding the properties that aer listed in teh employeeObject constructor signature. I also need to add a method named showEmployee.

    Then, I need to instantiate 3 instances of the employeeObject. The employees array is already there.

    Then, I need to complete the showEmployee function, by using the info variable that has already been declared. I need to concatenate a line break character after the name and department info.

    I have been all over the 'net trying to find someone to help me. This is so much info that I am drowning. I would GREATLY appreciate any help that anyone could give to me.


    Thank you

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here is what I need to do. I need to finish the employeeObject constructor, adding the properties that aer listed in teh employeeObject constructor signature.
    Code:
    function employeeObject(name,department,extension) {
    
    this.name = name;
    this.department = department;
    this.extension = extension;
    }
    It seems like you got it covered .

    By the way, the variable names for the arguments do not have to be the same as the object 'property' names. In fact, so that beginners are not confused, tutorials should make them different:

    Code:
    function employeeObject(n,d,e) {
    
    this.name = n;
    this.department = d;
    this.extension = e;
    }
    Just like a regular function, the argument names provide a way for you to refer to them inside the function. What is different about a constructor function is that in the function you see the 'this' keyword in lines like this:

    this.name = n;

    That line both creates the 'name' property for the object, and assigns the value 'n' to it. If you anticipate how you are going to access that property:

    alert(my_employeeObject.name);

    then the 'this' keyword refers to the particular employeeObject that is requesting the 'name' property.

    Here are some beginning object tutorials(OOP) that should get you through the rest of your script:

    http://www.sitepoint.com/article/oriented-programming-1
    http://www.webreference.com/js/column79/

    You need to fix this:

    Code:
    function employeeObject(name,department,extension) {
    
    this.name = name;
    this.department = department;
    this.extension = extension;
    }
    
    
    var employees = new Array();
    employees[1] = new productObject("Mai Li", "Sales", 551);
    employees[2] = new productObject("Maria Alvarez", "Human Resources", 441);
    employees[3] = new productObject("Tom Smith", "Marketing", 331);
    And, since all arrays start at index position 0, if you are going to skip the index 0 as you did above when you started with employee[1], then in your for-loops you have to acount for that.

    In the loop below, you are starting with 'i' equal to 0:
    Code:
    for (var i = 0; i < len; i++) {
    if(i == 0) document.write("<OPTION>" + employees[i]);
    else document.write("<OPTION>" + employees[i].name);
    }
    and in the loop you will refer to employee[0], but there is no employee[0], so employee[0] will be undefined, and you will get an error. You have two choices for fixing that problem. You can start the array at index position zero:

    employees[0] = ...
    employees[1] = ...
    employees[2] = ...

    which is recommended because then you can use for-loops in the format:

    var len=my_array.length;
    for(var i =0; i< len; i++)

    The other solution is to remember to start your loops at the same index position as you started your array, and adjust the loop condition so it ends after one additional iteration:

    var len=my_array.length;
    for(var i=1; i<=len; i++)
    Last edited by 7stud; Nov 13, 2004 at 03:16.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •