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

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 0.72 KB  |  hits: 12  |  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. Object.beget = (function(Function){
  2.     return function(Object){
  3.         Function.prototype = Object;
  4.         return new Function;
  5.     }
  6. })(function(){});
  7. /*
  8. It's a killer! Pity how almost no one uses it.
  9. It allows you to "beget" new instances of any object, extend them, while maintaining a (live) prototypical inheritance link to their other properties. Example:
  10. */
  11. var A = {
  12.   foo : 'greetings'
  13. };  
  14. var B = Object.beget(A);
  15.  
  16. alert(B.foo);     // 'greetings'
  17.  
  18. // changes and additionns to A are reflected in B
  19. A.foo = 'hello';
  20. alert(B.foo);     // 'hello'
  21.  
  22. A.bar = 'world';
  23. alert(B.bar);     // 'world'
  24.  
  25.  
  26. // ...but not the other way around
  27. B.foo = 'wazzap';
  28. alert(A.foo);     // 'hello'
  29.  
  30. B.bar = 'universe';
  31. alert(A.bar);     // 'world'