Last active
December 21, 2015 21:49
-
-
Save vstarck/6371420 to your computer and use it in GitHub Desktop.
decorator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pkg = {}; | |
pkg.foo = function(a, b, c) { | |
alert(a + ' ' + b + ' ' + c); | |
} | |
pkg.bar = function(a, b) { | |
alert(a + ' ' + b + ' '); | |
} | |
decorateMethods(pkg, ['foo', 'bar']) | |
function decorateMethods(object, methods) { | |
for(var i = 0, l = methods.length; i < l; i++) { | |
object[methods[i]] = reverseArguments(object[methods[i]], object); | |
} | |
} | |
function reverseArguments(fn, thisValue) { | |
return function() { | |
var args = []; | |
for(var i = 0, l = arguments.length; i < l; i++) { | |
args[i] = arguments[l - i - 1]; | |
} | |
fn.apply(thisValue, args); | |
} | |
} | |
pkg.foo(1, 2, 3); // "3 2 1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment