Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Proto = {
  2.   protoFn: new Function,
  3.  
  4.   clone: function()
  5.   {
  6.     Proto.protoFn.prototype = this;
  7.     var clone = new Proto.protoFn();
  8.     if (clone.init)
  9.     {
  10.       clone.init();
  11.     }
  12.     return clone;
  13.   }
  14. }
  15.  
  16. var Person = Proto.clone();
  17. Person.introduce = function(){ console.log(this.name) };
  18. Person.name = "Adam";
  19. Person.introduce(); //Adam
  20.  
  21.  
  22. var sean = Person.clone();
  23. sean.name = "Sean";
  24. sean.introduce(); //Sean
  25.  
  26.  
  27. var Smarty = Person.clone();
  28. Smarty.brains = 10;
  29. Smarty.introduce = function(){ console.log("I'm " + this.name + ". Turn it up to " + this.brains)
  30.  
  31. var rich = Smarty.clone();
  32. rich.name = "Rich"
  33.  
  34. //oops, forgot to update brains
  35. rich.introduce()
  36. "I'm Rich. Turn it up to 0"
  37. rich.brains = 11