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

How are the "class names" shown in Chrome's debugger determined? And what are they based

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.

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.

Share Improve this question asked Dec 18, 2013 at 21:04 David WoleverDavid Wolever 155k93 gold badges363 silver badges510 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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 pletely 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?

How is that "class name" determined?

See Chromium source code on github.

  • console-dir-es6 testcase
  • expected output (2022 update: I cannot locate the latest version of this file in HEAD -- but this is what you can use to determine whether or not you can shape the debugger output to your desires)

You can perhaps backtrack through the first page (might have to go through C code).

And, further, is it possible to control or modify?

You can parse the source of the class with .toString, do a .replace to change the name, and eval the modified source code. You now have a clone of the class that will display properly in the console inspector.

If you have no control over this (it's a third-party class), obviously don't do this, as the third party code may have references to the class. Also it creates a duplicate class; this would be confusing.

However, if you're doing a metaprogramming horror-show (and not for example for a job or anything that may see the light of day...), go ahead and use this. This is my personal tried and true method, until some better API appears. =\

I do not recall if there were performance implications of doing this

functions in Javascript can have name and when you define it like this:

function myfunc(){}

It is the same as if you do:

var myfunc = function myfunc(){}

in this function myfunc is your function's name, which is not unique, it means you can create different functions with the same name, of course you'd better not do that. and when you define your function like:

var myfunc = function(){}

you have created a function without any name, this would be an anonymous function and browser profilers have nothing to show you as the name of your function or as you have mentioned your class.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745608101a4635789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信