Function Names

Topics: General
Mar 24, 2014 at 5:04 AM
Is there a good reason why functions of classes do not keep their names in the exported JS? I don't understand why the constructor can have its name there, but the members cannot, which is a pain. It's not uncommon to pull a function name given the string representation of a function. This would, in a way, add runtime name information.
Mar 24, 2014 at 5:03 PM
Not sure I understand. Do you have a code example of what you mean?
Mar 24, 2014 at 5:52 PM
Edited Mar 25, 2014 at 7:39 PM
If you try looking at the produced JS of a class with methods you'd see what I mean:

Class A { func(){} }

The resulting JS shows

A.prototype.func = function(){};

And it should be

A.prototype.func = function func(){};
Mar 25, 2014 at 3:49 PM
Ah, I see. Though, I'm not clear in your example what added information it gives you in this situation. You are accessing "func" off the prototype, so you already know the name of the function.

That said, something we've been thinking about is enabling things like annotations/decorators to add more information to the objects you create and enable better ways of doing reflection.
Mar 25, 2014 at 7:50 PM
Edited Mar 25, 2014 at 8:18 PM
Currently when I get a function reference, I can use regex to pull the function name from the string. This just helps my API to build function names for things like serialization of handlers (given only an object and function reference), and error reporting when traversing the stack on exceptions (otherwise each function name in the stack is unknown, and I have to show function snipits [as a string] - this was originally my biggest issue with TS).

Consider this:
var d = new Delegate(object, "handler");
var d = new Delegate(object, object.handler);
Which is better? The last of course. Why? Because of refactoring, type checking, etc. would I even know the function's name? I need names in order to be able to serialize the delegates [objects have ID numbers], but currently I have to add the function names myself, but if the name existed as part of the function, that's more detail on the handler that makes all the difference.

I think reflection details are more important, and fits in line with attributes. That said, there would need to be options: function names only, function names and parameter types, class info, etc., so developers don't get what they don't need. Just keep in mind that annotations and decorations won't help my situation for the end user API at runtime (and forcing them to do it would not be good).