Catch-all proposal based on proxies

Maciej Stachowiak mjs at apple.com
Sat Dec 12 13:43:49 PST 2009


On Dec 12, 2009, at 11:08 AM, Mark S. Miller wrote:

> 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 
> >?

Interesting. A few of these uses appear to be in JavaScript libraries  
so they could be widespread. I see a few different uses:

- To get the function name
- To detect if something is a function at all (by seeing if the  
attempt to call it throws)
- To check for function equality (likely a bogus check!)
- To get argument names
- Something else mysterious that I couldn't figure out (looking for  
calls to the Function constructor inside the function body?)

Note though that in the case of DOM emulation specifically, probably  
most of these are not relevant. Looking at toString() on DOM methods  
is usually done for one of the following reasons:

1) To get the function name.
2) To check for "[native code]" to verify that a native method hasn't  
been replaced by JavaScript code (seems like a fairly bogus check).

Regards,
Maciej


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


More information about the es-discuss mailing list