如何在Chrome开发工具中为自定义类计算javascript类名称? [英] How are javascript class names calculated for custom classes in Chrome Dev Tools?

查看:59
本文介绍了如何在Chrome开发工具中为自定义类计算javascript类名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定在javascript中生成类名的规则.我将此脚本粘贴到了Chrome开发者工具控制台中:

I am trying to determine the rules for generating class names in javascript. I pasted this script into Chrome dev tools console:

var obj = { 
    Constr : function() {  }
};

var obj2 = obj;
console.log(new obj.Constr());
console.log(new obj2.Constr());

obj2.Constr2 = function() {  };
console.log(new obj.Constr2());
console.log(new obj2.Constr2());

这是控制台中的结果:

obj.Constr
obj.Constr
obj2.Constr2
obj2.Constr2

似乎类的名称由构造函数最初分配给的变量确定.我正在寻找CDT用于生成此名称的确切规则.另外,这是Google Closure Compiler可以识别的名字吗?

It seems that the name of the class is determined by the variable that the constructor function was originally assigned to. I am looking for the precise rules that CDT uses to generate this name. Also, is this the same name that the Google Closure Compiler recognizes?

我试图查看我是否可以在Firebug中重现类似的行为,但似乎无法在控制台中打印出类名.作为第二个问题,有人知道如何在Firebug中看到这一点吗?

I have tried to see if I can reproduce similar behavior in Firebug, but I can't seem to get class names to print out in the console. As a secondary question, does anyone know how to see this in firebug?

推荐答案

Javascript中没有类,因为它是基于原型的OOP,而不是基于类的. Chrome显然做了一些推论,以便在控制台中打印对象的某些描述,但这不是标准的Javascript-在标准中,对象没有命名的类,并且您无法弄清楚该对象所属的类的名称,因为唯一的继承是通过实际的[[Prototype]]内部伪属性完成的,所以它本身也是一个对象,没有名称或类".通常,您可以通过查看object.__proto__.constructor.name来推断类似于类名的内容,这将返回该函数的名称,该名称是实例化该对象的构造函数. 但是此函数可能是匿名的,或者您的浏览器可能不支持非标准的__proto__属性,的对象原型可能不包含对其构造函数的正确引用.通常,您无法了解JS中对象的类".您只能测试后代(object instanceof Constructor),但这仍然根据对象原型中的constructor属性来实现,而可能是不正确的.

There are no classes in Javascript, as it is prototype-based OOP, not class-based. Chrome apparently does some deducing in order to print some description of the object in the console, but that is not standard Javascript — in the standard, objects have no named class, and you cannot figure out the name of the class the object belongs to, since the only inheritance is done through the actual [[Prototype]] internal pseudo-property, which is also an object in its own right, with no name or "class". Usually, you might deduce something similar to a class name by looking at object.__proto__.constructor.name, which would return the name of the function which is the constructor from which the object was instantiated; but this function might be anonymous, or your browser might not support the non-standard __proto__ property, or the prototype of the object might not contain a correct reference to its constructor. Generally, you cannot know the "class" of an object in JS; you can only test for descendancy (object instanceof Constructor), but that is still implemented according to the constructor property in the object prototype, which might be incorrect.

这篇关于如何在Chrome开发工具中为自定义类计算javascript类名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆