Skip to content

Instantly share code, notes, and snippets.

@vstarck
Last active December 21, 2015 21:49
Show Gist options
  • Save vstarck/6371420 to your computer and use it in GitHub Desktop.
Save vstarck/6371420 to your computer and use it in GitHub Desktop.
decorator.js
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