Skip to content

Instantly share code, notes, and snippets.

@dcherman
Created May 28, 2013 20:05
Show Gist options
  • Select an option

  • Save dcherman/5665673 to your computer and use it in GitHub Desktop.

Select an option

Save dcherman/5665673 to your computer and use it in GitHub Desktop.
if ( !Object.create ) {
Object.create = function( parentProto ) {
function F() {}
F.prototype = parentProto;
return new F();
}
}
function Animal() {}
Animal.prototype.speak = function() {
throw new Error( "Not implemented" );
};
Animal.extend = function( proto ) {
var i;
function Child() {}
Child.prototype = Object.create( this.prototype );
for( i in proto ) {
Child.prototype[ i ] = proto[ i ];
}
Child.extend = this.extend;
return Child;
}
var Dog = Animal.extend({
speak: function() {
console.log( "WOOF" );
}
});
var dog = new Dog();
dog.speak();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment