Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. A = function(){
  2. alert("I'm a naive function");
  3. };
  4. B = function(){
  5. alert("I'm having a piggyback ride on function A!"+
  6. "And the fool doesn't even know it!");
  7. };
  8. addHook(B, A)//add hook B to function A
  9. A()
  10. //getting alerts "I'm a naive function"/"I'm having a
  11. //piggyback ride on function A! And the fool doesn't even know it!"
  12.  
  13. <script>
  14.  
  15. function A(x) { alert(x); return x; }
  16. function B() { alert(123); }
  17.  
  18. function addHook(functionB, functionA, parent)
  19. {
  20. if (typeof parent == 'undefined')
  21. parent = window;
  22.  
  23. for (var i in parent)
  24. {
  25. if (parent[i] === functionA)
  26. {
  27. parent[i] = function()
  28. {
  29. functionB();
  30. return functionA.apply(this, arguments)
  31. }
  32.  
  33. break;
  34. }
  35. }
  36. }
  37.  
  38. addHook(B, A);
  39.  
  40. A(2);
  41.  
  42. </script>
  43.  
  44. function someFunction() { alert("Bar!") }
  45. var placeholder=someFunction;
  46. someFunction=function() {
  47. alert("Foo?");
  48. placeholder();
  49. }
  50.  
  51. var function_itself = function() {
  52. alert('in function itself');
  53. }
  54. function_itself.PRE_PROCESS = function() {
  55. alert('in pre_process');
  56. }
  57. function_itself.POST_PROCESS = function() {
  58. alert('in post_process');
  59. }
  60.  
  61. var function_processor = function(func) {
  62. if (func.PRE_PROCESS) {
  63. func.PRE_PROCESS.call();
  64. }
  65. func.call();
  66. if (func.POST_PROCESS) {
  67. func.POST_PROCESS.call();
  68. }
  69. }
  70.  
  71. // usage:
  72. /*
  73. function test(x) {
  74. alert(x);
  75. }
  76.  
  77. var htest = hookable(test);
  78.  
  79. htest.addHook("before", function (x) {
  80. alert("Before " + x);
  81. })
  82. htest.addHook("after", function (x) {
  83. alert("After " + x);
  84. })
  85.  
  86. htest("test") // => Before test ... test ... After test
  87. */
  88. function hookable(fn) {
  89. var ifn = fn,
  90. hooks = {
  91. before : [],
  92. after : []
  93. };
  94.  
  95. function hookableFunction() {
  96. var args = [].slice.call(arguments, 0),
  97. i = 0,
  98. fn;
  99. for (i = 0; !!hooks.before[i]; i += 1) {
  100. fn = hooks.before[i];
  101. fn.apply(this, args);
  102. }
  103. ifn.apply(this, arguments);
  104. for (i = 0; !!hooks.after[i]; i++) {
  105. fn = hooks.after[i];
  106. fn.apply(this, args);
  107. }
  108. }
  109.  
  110. hookableFunction.addHook = function (type, fn) {
  111. if (hooks[type] instanceof Array) {
  112. hooks[type].push(fn);
  113. } else {
  114. throw (function () {
  115. var e = new Error("Invalid hook type");
  116. e.expected = Object.keys(hooks);
  117. e.got = type;
  118. return e;
  119. }());
  120. }
  121. };
  122.  
  123. return hookableFunction;
  124. }
Add Comment
Please, Sign In to add comment