Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. function Shape() {}
  2. function Circle() {}
  3.  
  4. // Prototypical inheritance
  5. Circle.prototype = Object.create(Shape.prototype);
  6. Circle.prototype.constructor = Circle;
  7.  
  8. function Rectangle(color) {
  9. // To call the super constructor
  10. Shape.call(this, color);
  11. }
  12.  
  13. // Method overriding
  14. Shape.prototype.draw = function() {};
  15. Circle.prototype.draw = function() {
  16. // Call the base implementation
  17. Shape.prototype.draw.call(this);
  18.  
  19. // Do additional stuff here
  20. };
  21.  
  22. // Don't create large inheritance hierarchies.
  23. // One level of inheritance is fine.
  24.  
  25. // Use mixins to combine multiple objects
  26. // and implement composition in JavaScript.
  27. const canEat = {
  28. eat: function() {}
  29. };
  30.  
  31. const canWalk = {
  32. walk: function() {}
  33. };
  34.  
  35. function mixin(target, ...sources) {
  36. // Copies all the properties from all the source objects
  37. // to the target object.
  38. Object.assign(target, ...sources);
  39. }
  40.  
  41. function Person() {}
  42.  
  43. mixin(Person.prototype, canEat, canWalk);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement