Last active
August 29, 2015 14:09
-
-
Save franzwong/bbb1fa13ca37da7d6003 to your computer and use it in GitHub Desktop.
Javascript Inheritance
This file contains hidden or 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
// 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