Created
September 7, 2011 02:58
-
-
Save cms/1199648 to your computer and use it in GitHub Desktop.
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
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