...

View Full Version : *Please can someone review* Writing an Object Constructor with Inheritance



dcskye
01-16-2012, 12:14 PM
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));

DaveyErwin
01-16-2012, 01:12 PM
[QUOTE=dcskye;1181429]I have been trying to make sense of certain codes so that I can increase my understanding.
QUOTE]


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum