Guest User

Untitled

a guest
Jul 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. // an IO-like clone system for javascript. thoughts?
  2.  
  3. Object.prototype.clone = function() {
  4. return Object.create(this);
  5. }
  6.  
  7. Object.prototype.merge = function(obj) {
  8. var
  9. keys = Object.keys(obj),
  10. self = this
  11. ;
  12. keys.forEach(function(key) {
  13. self[key] = obj[key]
  14. });
  15. return this;
  16. }
  17.  
  18. Object.prototype.inherit = function(obj) {
  19. return this.clone().merge(obj);
  20. }
  21.  
  22. mySharedPrototpe = {
  23. a: 1,
  24. b: function() { return 2; }
  25. //etc
  26. }
  27.  
  28. function MyConstructor() {
  29. // ...
  30. }
  31. MyConstructor.prototype = mySharedPrototype.inherit({
  32. a: 4,
  33. c: "LOL"
  34. });
  35.  
  36. o = new MyConstructor();
  37. o.a // => 4
  38. o.b // => function() { return 2; }
  39. o.c // => "LOL"
Add Comment
Please, Sign In to add comment