name property for built-in functions??

Brendan Eich brendan at mozilla.com
Sun Mar 1 10:56:01 PST 2009


On Mar 1, 2009, at 10:28 AM, Allen Wirfs-Brock wrote:

> From: Brendan Eich [mailto:brendan at mozilla.com]
>> What should (new Function).name or (equivalently) Function().name  
>> return? Precedent in some engines:
>>
>> js> (new Function).name
>> anonymous
>>
>> An anonymous function expression returns the empty string in some  
>> implementations:
>>
>> js> (function(){}).name
>>
>> js> typeof (function(){}).name
>> string
>> js> (function(){}).name.length
>> 0
>
> I have a hard time buying (new Function).name === "anonymous" when  
> (function(){}).name==="".

Me too, FWIW. I don't think this Function-constructor case is as  
important as the anonymous function expression one.


> While it may go against precedent I think it would be more useful if  
> (new Function).name === "new Function".

That's better than "anonymous". I'm not so sure it's better than "" or  
undefined, based on use-case arguments below.


>  I can live with (function(){}).name==="" although (function() 
> {}).name==="anonymous function" would probably be better.

If the use-case is diagnostics of the form "unexpected argument to " +  
funarg.name, then you're right. If the use-case is forking control  
flow based on anonymous vs. named function expression (or definition)  
then "" or even undefined is better.

Without more evidence, I would be in favor of a concise "no name"  
value, and since at least some implementations (Rhino, SpiderMonkey --  
not sure about IE, can't test at home) use "", "" beats undefined by  
that criterion.

Using undefined as the "no name" value happens to match ES3 results  
for any function's name property, but that does not seem helpful for  
cross-browser scripts. Any new use of the name property might want to  
be conditional (a la "object detection"), even for anonymous functions.

It's hard to be sure without more user feedback, though.

/be


More information about the Es-discuss mailing list