Skip to content

Instantly share code, notes, and snippets.

@franzwong
Last active August 29, 2015 14:09
Show Gist options
  • Save franzwong/bbb1fa13ca37da7d6003 to your computer and use it in GitHub Desktop.
Save franzwong/bbb1fa13ca37da7d6003 to your computer and use it in GitHub Desktop.
Javascript Inheritance
// Some browsers do not support Object.create
if (typeof Object.create === 'undefined') {
Object.create = function(prototype) {
function C() {}
C.prototype = prototype;
return new C();
}
}
function Actor(name) {
this.name = name;
}
Actor.prototype.greet = function(name) {
console.log('hello, ' + name + '. I am ' + this.name);
}
function Staff(name) {
Actor.call(this, name);
}
Staff.prototype = Object.create(Actor.prototype);
var staff = new Staff('franz');
staff.greet('guest');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment