代码之家  ›  专栏  ›  技术社区  ›  Alexander Nenashev

Object.setPrototypeOf:如何获取原始原型名称?

  •  1
  • Alexander Nenashev  · 技术社区  · 2 年前

    我在构造函数中更改了类实例的原型。新的原型是 Object 的实例。因此,当我尝试使用 self.__proto__.constructor.name 它返回 对象 这是正确的。
    但Chrome Devtools不知何故知道 self 仍然是的一个实例 Example !
    我记得Chrome Devtools知道JS中无法访问的一些东西。
    JS中有没有像Chrome Devtools那样检索对象原始构造函数名称的方法?

    顺便说一句,这个代码纯粹是实验性的,不适合生产。

    enter image description here

    const example = new function Example() {
        const self = Object.setPrototypeOf(this, {
            get name(){
                return self.__proto__.constructor.name;
            },
        });
    };
    
    console.log(example.name);
    0 回复  |  直到 2 年前