How are javascript class names calculated for custom classes in Chrome Dev Tools?

前端 未结 1 646
醉酒成梦
醉酒成梦 2021-01-03 01:15

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 : f         


        
相关标签:
1条回答
  • 2021-01-03 02:11

    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.

    0 讨论(0)
提交回复
热议问题