This is something which has been bugging me with the Google Chrome debugger and I was wondering if there was a way to solve it.
I'm working on a large Javascript application, using a lot of object oriented JS (using the Joose framework), and when I debug my code, all my classes are given a non-sensical initial display value. To see what I mean, try this in the Chrome console:
var F = function () {};
var myObj = new F();
console.log(myObj);
The output should be a single line which you can expand to see all the properties of myObj
, but the first thing you see is just ▶ F
.
My issue is that because of my OO framework, every single object instantiated gets the same 'name'. The code which it looks is responsible for this is like so:
getMutableCopy : function (object) {
var f = function () {};
f.prototype = object;
return new f();
}
Which means that in the debugger, the initial view is always ▶ f
.
Now, I really don't want to be changing anything about how Joose instantiates objects (getMutableCopy...?), but if there was something I could add to this so that I could provide my own name, that would be great.
Some things that I've looked at, but couldn't get anywhere with:
> function foo {}
> foo.name
"foo"
> foo.name = "bar"
"bar"
> foo.name
"foo" // <-- looks like it is read only
I think this is the best way to dynamically set the name of a function :
Now you just need to call the
setName
methodBased on the answer of @josh, this prints in a console REPL, shows in console.log and shows in the debugger tooltip:
Inclusion of fn.oldToString() is a magic which makes it work. If I exclude it, nothing works any more.
Similar to @Piercey4 answer, but I had to set the
name
for the instance as well:This won't totally solve your problem, but I would suggest overriding the toString method on the class's prototype. For instance:
You'll still see the original class name if you enter an instance of my_class directly in the console (I don't think it's possible to do anything about this), but you'll get the nice name in error messages, which I find very helpful. For instance:
Will give the error message: "TypeError: Object Name of Class has no method 'does_not_exist'"
I've been playing around with this for the last 3 hours and finally got it at least somewhat elegant using new Function as suggested on other threads:
If you run the above code, you should see the following output to your console:
Combine usage of computed property name to dynamically name a property, and inferred function naming to give our anonymous function that computed property name:
One could use whatever they want for 'name' here, to create a function with whatever name they want.
If this isn't clear, let's break down the two pieces of this technique separately:
Computed Property Names
We can see that the property name assigned on
o
wasmyProperty
, by way of computed property naming. The[]
's here cause JS to lookup the value inside the bracket, and to use that for the property name.Inferred Function Naming
Here we use inferred function naming. The language looks at the name of wherever the function is being assigned to, & gives the function that inferred name.
We can combine these two techniques, as shown in the beginning. We create an anonymous function, which gets it's name via inferred function naming, from a computed property name, which is the dynamic name we wanted to create. Then we have to extract the newly created function from the object it is embedded inside of.