"Pretty" function expression names

P T Withington ptw at pobox.com
Mon May 4 14:58:31 PDT 2009

On 2009-05-04, at 17:39EDT, Brendan Eich wrote:

> On May 4, 2009, at 2:00 PM, P T Withington wrote:
>> On 2009-05-04, at 14:46EDT, Brendan Eich wrote:
>>> On May 4, 2009, at 10:45 AM, P T Withington wrote:
>>>> Assuming we're allowed to speculate on futures here...
>>>> I was thinking about improving debug-ability by adding  
>>>> descriptive names to function expressions thusly,
>>>> var myFun = function "my description here" (...) { ... };
>>> Is this better for your purposes than
>>> var myFun = function (...) { ... };
>>> myFun.prettyName = "my description here";
>> Just that it's shorter and easier to type.  It would be a shorthand  
>> notation for setting the .name property of the function object  
>> created.  I wouldn't expect the function to be referenceable by the  
>> name, but I'd want Function.toString to display it (as a string  
>> literal).
> As Michael Haufe and Mike Wilson point out, this might leave some  
> wanting to call the function by that name within its own body. I'm  
> interested in why you wouldn't care about that use case -- because  
> you can guarantee that myFun never varies?
> An intrinsic name for toString and reflection is a fine thing, but  
> people tend to expect functions with names to be callable in some  
> scope. There's the rub.

My real use case is passing an anonymous (non-recursive) function as  
an argument.  I don't actually know what identifier my function will  
be bound to when it is called.  If I need a recursive function _and_ a  
pretty name, presumably I could use either your proposal or:

   let x = function "pretty name" () { ... x() ... }

I see that people might expect to be able to say:

   "pretty name"()

but I wasn't asking for that.  (OTOH, there is precedent for using  
string literals for attribute names that are not valid identifiers.)

> I'm adding strawman and harmony pages to the wiki based on existing  
> discussions, so I'll roll up the function name proposal thread and  
> add this item. Thanks.


More information about the es-discuss mailing list