How are the "class names" shown in Chrome's debugger determined? And what are they based on?
For example, with function Foo() {}, the "class name" is fairly obvious:
> function Foo() {}
> new Foo()
Foo {} // shows the "class name" Foo
It would seem that this controlled by the .name attribute, but that is not the case:
> Foo.name = "NotFoo"
> new Foo()
Foo {} // shows "Foo", not "NotFoo"
But sometimes it can be very surprising:
> function subclass(base, methods) {
var initializer = methods.__init__;
initializer.prototype = $.extend(initializer.prototype, base.prototype, methods);
return initializer;
}
> Bar = subclass(Object, { __init__: function() {} });
> new Bar()
subclass.__init__ {__init__: function} // Shows "subclass.__init__"… ?!
How is that "class name" determined?
And, further, is it possible to control or modify?
This seems to be specific to Chrome, as Firefox doesn't appear to try and guess the "class name" in the second example.
Google Chrome's official website does not help on this, but I point you to this SO:
How are javascript class names calculated for custom classes in Chrome Dev Tools?
I don't think you can override chrome console's "deducing" mechanism.
some console play:
>>function Foo(){}
>>function NotFoo(){}
>>var a = new Foo();
>>a.constructor = NotFoo;
>>a
Foo {constructor: function}
constructor: function NotFoo(){}
__proto__: Foo
>> a instanceof Foo
true
>> a.__proto__.constructor.name
"Foo"
>> a.__proto__.constructor.name = "NotFoo"
"NotFoo"
>> a
Foo {constructor: function}
>> a instanceof Foo
true
If chrome developers wanted to make it completely unoverridable, you can see how they could've made a check based on webkit's implementation of instanceof...
and: How to check the class of an instance in Javascript?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With