1 // ==========================================
2 // Copyright 2013 Twitter, Inc
3 // Licensed under The MIT License
4 // http://opensource.org/licenses/MIT
5 // ==========================================
18 around: function(base, wrapped) {
19 return function composedAround() {
20 // unpacking arguments by hand benchmarked faster
21 var i = 0, l = arguments.length, args = new Array(l + 1);
22 args[0] = base.bind(this);
23 for (; i < l; i++) args[i + 1] = arguments[i];
25 return wrapped.apply(this, args);
29 before: function(base, before) {
30 var beforeFn = (typeof before == 'function') ? before : before.obj[before.fnName];
31 return function composedBefore() {
32 beforeFn.apply(this, arguments);
33 return base.apply(this, arguments);
37 after: function(base, after) {
38 var afterFn = (typeof after == 'function') ? after : after.obj[after.fnName];
39 return function composedAfter() {
40 var res = (base.unbound || base).apply(this, arguments);
41 afterFn.apply(this, arguments);
46 // a mixin that allows other mixins to augment existing functions by adding additional
47 // code before, after or around.
48 withAdvice: function() {
49 ['before', 'after', 'around'].forEach(function(m) {
50 this[m] = function(method, fn) {
52 compose.unlockProperty(this, method, function() {
53 if (typeof this[method] == 'function') {
54 this[method] = advice[m](this[method], fn);