Skip to content

Instantly share code, notes, and snippets.

@t0dd
Created May 10, 2013 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0dd/5552781 to your computer and use it in GitHub Desktop.
Save t0dd/5552781 to your computer and use it in GitHub Desktop.
Just a simple demo of prototype chaining & inheritance I find useful.
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