Skip to content

Instantly share code, notes, and snippets.

@cms
Created September 7, 2011 02:58
Show Gist options
  • Save cms/1199648 to your computer and use it in GitHub Desktop.
Save cms/1199648 to your computer and use it in GitHub Desktop.
var wrap = function (fn, wrapper) {
var params = [], len = fn.length;
while (len--) {
params[len] = 'a'+len;
}
return Function('fn,wrapper',
'return function ('+params+') {' +
' return wrapper.call(this, fn, [].slice.call(arguments));'+
'}')(fn, wrapper);
};
function a(x, y, z) {
console.log(x, y, z);
}
a(1, 2, 3); // 1,2,3
a.length; // 3
var b = wrap(a, function (origFn, args) {
console.log(args); // [1,2,3]
console.log(origFn === a); // true
return origFn.apply(this, args); // 1, 2, 3
});
b(1,2,3);
b.length; // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment