Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "class name" shown in Chrome's debugger based on? Can it be controlled?

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.

like image 969
David Wolever Avatar asked Oct 16 '25 15:10

David Wolever


1 Answers

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?

like image 78
TastySpaceApple Avatar answered Oct 18 '25 03:10

TastySpaceApple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!