The Decorator Pattern for JavaScript


The Decorator Pattern for JavaScript

  • Run actsAsDecorator() on any object.
  • This object will now have a before() and after() method that work like this:
    o object.before(‘somethingHappens’, function() { });
    o object.after(‘someMethodGetsCalled’, function() { });
  • Now, whenever you call object.somethingHappens(), the function you passed in will be called before the original function runs.
  • Similarly, whenever object.someMethodGetsCalled gets called, that other function you passed in will be executed afterwards.

Just remember: before(‘method’, function() { }); after(‘method’, function() { });

Related Posts