Skip to content

Instantly share code, notes, and snippets.

@richcollins
Forked from anonymous/gist:1100327
Created July 22, 2011 22:27
Show Gist options
  • Save richcollins/1100576 to your computer and use it in GitHub Desktop.
Save richcollins/1100576 to your computer and use it in GitHub Desktop.
Proto = {
protoFn: new Function,
clone: function()
{
Proto.protoFn.prototype = this;
var clone = new Proto.protoFn();
if (clone.init)
{
clone.init();
}
return clone;
}
}
var Person = Proto.clone();
Person.introduce = function(){ console.log(this.name) };
Person.name = "Adam";
Person.introduce(); //Adam
var sean = Person.clone();
sean.name = "Sean";
sean.introduce(); //Sean
var Smarty = Person.clone();
Smarty.brains = function(){ throw new Error("Override Me") }
Smarty.introduce = function(){ console.log("I'm " + this.name + ". Turn it up to " + this.brains())
var rich = Smarty.clone();
rich.name = "Rich"
//oops, forgot to update brains
rich.introduce() //exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment