Advertisement
Guest User

Untitled

a guest
May 5th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. 'use strict';
  2.  
  3. function Mule (obj, ...args) {
  4. const mule = new Proxy({}, {
  5. get: function (target, prop, receiver) {
  6. const topKeys = Object.keys(target);
  7. if (topKeys.includes(prop)) {
  8. return target[prop];
  9. }
  10.  
  11. if (!obj.mule) {
  12. return undefined;
  13. }
  14.  
  15. const muleKeys = Object.keys(obj.mule);
  16. if (muleKeys.includes(prop)) {
  17. return obj.mule[prop];
  18. }
  19.  
  20. return undefined;
  21. }
  22. });
  23. obj.apply(mule, args);
  24.  
  25. return mule;
  26. };
  27.  
  28. Mule.inherits = function (child, parent) {
  29. child.mule = Object.assign({}, parent.mule);
  30. };
  31.  
  32.  
  33. const Hello = function () {
  34. this.hello = function () {
  35. console.log('hello ');
  36. };
  37. };
  38.  
  39. Hello.mule = {};
  40.  
  41. Hello.mule.world = function () {
  42. console.log('world');
  43. };
  44.  
  45. const Foo = function (...args) {
  46. Hello.apply(this, args);
  47. }
  48. Mule.inherits(Foo, Hello);
  49.  
  50. Foo.mule.bar = function () {
  51. console.log('bar');
  52. };
  53.  
  54. const hello = Mule(Hello);
  55. hello.hello();
  56. hello.world();
  57. console.log(hello.bar);
  58.  
  59.  
  60. const foo = Mule(Foo);
  61. foo.hello();
  62. foo.world();
  63. foo.bar();
  64.  
  65.  
  66.  
  67.  
  68. // Output
  69. /*
  70. hello
  71. world
  72. undefined
  73. hello
  74. world
  75. bar
  76. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement