code_junkie

Overriding a JavaScript function while referencing the original

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. function a()
  2. {
  3. new_code();
  4. original_a();
  5. }
  6.  
  7. function a()
  8. {
  9. original_a();
  10. other_new_code();
  11. }
  12.  
  13. var a = (function() {
  14. var original_a = a;
  15.  
  16. if (condition) {
  17. return function() {
  18. new_code();
  19. original_a();
  20. }
  21. }
  22. else {
  23. return function() {
  24. original_a();
  25. other_new_code();
  26. }
  27. }
  28. })();
  29.  
  30. //Saving the original func
  31.  
  32. var org_foo = window.foo;
  33.  
  34. //Assigning proxy fucnc
  35.  
  36. window.foo = function(args){
  37.  
  38. //Performing checks
  39.  
  40. if(checkCondition(args)){
  41.  
  42. //Calling original funcs
  43.  
  44. org_foo(args);
  45. }
  46. };
  47.  
  48. if (condition) { new_code(); original_a(); }
  49. else { original_a(); other_new_code(); }
  50. });
Add Comment
Please, Sign In to add comment