Preserving arity in function wrappers

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Sep 16 10:41:19 PDT 2013


when I've seen this ...


On Sat, Sep 14, 2013 at 1:49 PM, Bruno Jouhier <bjouhier at gmail.com> wrote:

>
> * inside wrap, call F.toString(), replace F() by F(a1, a2, ...), eval and
> return the new function.
>
>

I wondered why this would not be enough ...

```javascript
function wrap(Function) {
  for(var i = 0, a = []; i < Function.length; a[i] = 'a' + i++);
  return eval('('.concat(
    'function ',
    Function.name || '',
    '(', a.join(','), ') {',
      // do some magic here
      'return Function.apply(this, arguments);',
    '})'
  ));
}

function first(one, two, three) {
  return one + two + three;
}

wrap(first)(1, 2, 3); // 6
```

... if you parse to string you loose the scope plus most testing frameworks
out there use the with statement, eval, Function, etc ... so why do you
feel like eval is not a solution? Is is a JSLint VS testing code the
problem or something else I cannot think about right now?

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130916/ccc03d81/attachment.html>


More information about the es-discuss mailing list