Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Location
    USA
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question *Please can someone review* Writing an Object Constructor with Inheritance

    I have been trying to make sense of certain codes so that I can increase my understanding.


    I am trying to write an “Engineer” object that inherits from the “Employee” object. The Engineer object constructor should take the same arguments as the employee constructor as well as a list of projects the engineer is working on. The idea is that the Engineer constructor should also appropriately call its parent’s constructor and implement a show method that calls the parent show method first and then prints the project list afterwards. In the code an example usage of both classes follows the implementation

    This is the code I am using, it is as follows:


    // Employee Type
    function Employee(name, title) {
    this.name = name;
    this.title = title || "lackey";
    }

    Employee.prototype.show = function() {
    console.log("Name: " + this.name);
    console.log("Title: " + this.title);
    };

    // IMPLEMENT Engineer OBJECT HERE

    // Example usage...
    var dave = new Employee("Dave", "Developer");
    var kurt = new Engineer("Kurt", "Lead Developer", [ "SMS", "Project1" ]);

    dave.show();
    kurt.show();

    // This should be TRUE!
    console.log("Kurt is an Employee also? " + (kurt instanceof Employee));

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    [QUOTE=dcskye;1181429]I have been trying to make sense of certain codes so that I can increase my understanding.
    QUOTE]

    Code:
    <script> 
     
    // Employee Type
    function Employee(name, title) {
    this.name = name;
    this.title = title || "lackey";
    }
     
    Employee.prototype.show = function() {
    alert("Name: " + this.name);
    alert("Title: " + this.title);
    };
     
    // IMPLEMENT Engineer OBJECT HERE
    Engineer.prototype = new Employee();
    function Engineer(name,title,projects){
    	Employee.call(this,name,title);
    	this.projects = projects;
    }
    // Example usage...
    var dave = new Employee("Dave", "Developer");
    var kurt = new Engineer("Kurt", "Lead Developer");
     
    dave.show();
    kurt.show();
     
    // This should be TRUE!
    alert("Kurt is an Employee also? " + (kurt instanceof Employee)); 
     
    </script>

  • Users who have thanked DaveyErwin for this post:

    dcskye (01-16-2012)


  •  

    Posting Permissions

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