简体   繁体   English

Chrome 调试器中显示的“类名”是基于什么? 可以控制吗?

[英]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? Chrome 调试器中显示的“类名”是如何确定的? And what are they based on?它们基于什么?

For example, with function Foo() {} , the "class name" is fairly obvious:例如,对于function Foo() {} ,“类名”是相当明显的:

> 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:这似乎是由.name属性控制的,但事实并非如此:

> 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.这似乎是 Chrome 特有的,因为 Firefox 似乎没有尝试猜测第二个示例中的“类名”。

Google Chrome's official website does not help on this, but I point you to this SO:谷歌浏览器的官方网站对此没有帮助,但我向你指出这个 SO:

How are javascript class names calculated for custom classes in Chrome Dev Tools? 如何在 Chrome Dev Tools 中为自定义类计算 javascript 类名?

I don't think you can override chrome console's "deducing" mechanism.我认为您无法覆盖 chrome 控制台的“推导”机制。

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 ...如果 chrome 开发人员想让它完全不可覆盖,您可以看到他们如何根据 webkit 的instanceof实现进行检查...

and: How to check the class of an instance in Javascript?和: 如何在 Javascript 中检查实例的类?

How is that "class name" determined?这个“类名”是如何确定的?

See Chromium source code on github.请参阅 github 上的 Chromium 源代码。

You can perhaps backtrack through the first page (might have to go through C code).您也许可以回溯到第一页(可能必须通过 C 代码)。

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.您可以使用 .toString 解析类的源代码,执行 .replace 更改名称,并评估修改后的源代码。 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.这是我个人尝试过的真实方法,直到出现更好的 API。 =\\ =\\

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: Javascript 中的函数可以有名字,当你像这样定义它时:

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.在这个函数中, myfunc是你的函数名,它不是唯一的,这意味着你可以创建不同的同名函数,当然你最好不要这样做。 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 .您创建了一个没有任何名称的函数,这将是一个匿名函数,浏览器分析器没有任何内容可以显示您的函数名称或您提到的class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM