Get parent class name from child with ES6?

前端 未结 4 1390
野性不改
野性不改 2020-11-29 08:16

I would like to get the parent class name (Parent), but I\'m only able to retrieve the child class name with this code (Child)...

4条回答
  •  青春惊慌失措
    2020-11-29 08:46

    You could technically do

    // instanceProto === Child.prototype
    var instanceProto = Object.getPrototypeOf(instance);
    
    // parentProto === Parent.prototype
    var parentProto = Object.getPrototypeOf(instanceProto);
    console.log(parentProto.constructor.name);
    

    keep in mind that these names may all be mangled by minifiers.

提交回复
热议问题