Catch-all proposal based on proxies

Mike Samuel mikesamuel at gmail.com
Sat Dec 12 17:16:26 PST 2009


2009/12/12 Mark S. Miller <erights at google.com>:
> On Sat, Dec 12, 2009 at 10:53 AM, Mike Samuel <mikesamuel at gmail.com> wrote:
>>
>> On the interaction of Function.prototype.toString and function
>> proxies, one use case is code that tries to get at a function's name
>> as by doing
>>
>> function nameOf(f) {
>>  if ('name' in f) { return f.name; }  // Works on some interpreters
>>  var m = ('' + f).match(/^function\s+([^(\s]+)/);
>>  return m ? m[1] : void 0;
>> }
>>
>> I'm not sure how much of the existing code like this invokes toString
>> directly or indirectly instead of using Function.prototype.toString.
>>
>> Function proxy handlers could implement has('name') then there might
>> not be a need for Function.prototype.toString to support this use
>> case.
>>
>
> Under the current proposal, a trapping function proxy f can virtualize all
> the following without problem:
>     'name' in f
>     f.name
>     '' + f
>     f.toString()
> The only open issue is
>     Function.prototype.toString.call(f)
> I would have ventured a guess that this isn't used in real code. But having
> learned my lesson ;), I looked. What do we all think
> of <http://www.google.com/codesearch?hl=en&lr=&q=Function.prototype.toString.call+lang:javascript&sbtn=Search>?

So in your search results, prototype is extracting formal parameter names.
  argumentNames: function() {
    var names = Function.prototype.toString.call(this)
     .match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1]
     .replace(/\s+/g, '').split(',');
    return names.length == 1 && !names[0] ? [] : names;
  },

> --
>    Cheers,
>    --MarkM
>


More information about the es-discuss mailing list