JS implements interception method calls

JS implements interception method calls

  1. /**
  2. * JS method intercepts the method, as long as false is returned, the following method will no longer be executed
  3. * Example of calling:
  4. * func = func.before(beforefn).after(afterfn);
  5. * func();
  6. */
  7. Function.prototype.before = function(beforefn){
  8. var _self = this;
  9. return function(){
  10. if(beforefn.apply(this,arguments) === false){
  11. return false;
  12. }
  13. return _self.apply(this,arguments);
  14. }
  15. }
  16. Function.prototype.after = function(afterfn){
  17. var _self = this;
  18. return function(){
  19. var ret = _self.apply(this,arguments);
  20. if (ret === false) {
  21. return false;
  22. }
  23. afterfn.apply(this,arguments);
  24. return ret;
  25. }
  26. }

 

 

Related: JS implements interception method calls