name property for built-in functions??

Maciej Stachowiak mjs at apple.com
Tue Mar 3 14:39:37 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==="".  I think it is fine to have the name  
> distinguish functions created using new Function from functions  
> created via function expressions that don't have an explicit name.   
> However, the concept of "anonymous" could reasonably be applied to  
> either so using it for only one of them is likely to create  
> confusion for programmer to don't deeply understand this difference.  
> Plus, "anonymous" could actually be the declared name of a function.
>
> In addition, at the Mt. View meeting we decide that if the target  
> function of a bind call has the empty string as its name then the  
> name of the function that is return from the bind is "bind anonymous  
> function".  That would add to the confusion.
>
> While it may go against precedent I think it would be more useful if  
> (new Function).name === "new Function".  I can live with (function() 
> {}).name==="" although (function(){}).name==="anonymous function"  
> would probably be better.

Surprising though it may be, it seems that (new Function).name ===  
"anonymous" may be relevant to Web compatibility. We changed  
JavaScriptCore to match SpiderMonkey on this as a result of this bug:

http://bugzilla.opendarwin.org/show_bug.cgi?id=7726

While it's true that the code relying on the name may be completely  
bogus, or that the specific string "anonymous" may only matter in the  
Function.prototype.toString serialization and not in the name  
property, I think the potential improvement is not great enough to be  
worth the risk.

Note: this was not what I expected to find when I went spelunking in  
our bug database; I thought we just blindly followed Gecko here before  
I did the research.

I strongly suspect code also depends on (function(){}).name==="" but I  
don't have a clear-cut example handy.

Regards,
Maciej


Regards,
Maciej



More information about the Es-discuss mailing list