Created
May 10, 2013 06:45
-
-
Save t0dd/5552781 to your computer and use it in GitHub Desktop.
Just a simple demo of prototype chaining & inheritance I find useful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(firstName, lastName){ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
}; | |
Person.prototype.getFullName = function(){ | |
return this.firstName + " " + this.lastName; | |
}; | |
function Employee(firstName, lastName, position){ | |
Person.call(this, firstName, lastName); //inherits properties from Person() | |
this.position = position; | |
}; | |
Employee.prototype = new Person(); //Inherits Person() prototype | |
Employee.prototype.getFullName = function() { | |
var fullName = Person.prototype.getFullName.call(this); //calling line 6 as if it were an Employee() | |
return fullName + ", " + this.position; | |
}; | |
var employee = new Employee("Foo", "Boy", "Pain Enhancer"); | |
alert(employee.getFullName() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment