将基类添加到现有的原型链中,以便instanceof起作用 [英] Add base class to existing prototype chain so that instanceof works

查看:74
本文介绍了将基类添加到现有的原型链中,以便instanceof起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的原型层次结构,我想对其进行修改,以使层次结构保持完整,但是在其末尾添加了一个附加的原型. instanceof对于所有原型都应返回true.

I have an existing prototype hierarchy and I want to modify it so that the hierarchy is kept intact but an additional prototype is added to then end of it. instanceof should return true for all prototypes.

即:说我有B-> A,而我想将其设置为B-> A-> Base.现在instanceof对于A,B,Base应该返回true.

I.e.: say I have B->A and I want to make it B->A->Base. Now instanceof should return true for A, B, Base.

我尝试使用B.prototype.prototype和Object.setPrototypeOf(),但在两种情况下都没有运气.

I tried using B.prototype.prototype and Object.setPrototypeOf(), but no luck in either case.

使用Object.setPrototypeOf()进行采样:

Sample with Object.setPrototypeOf():

class A {                                                                                                                                                                                                                            
    do() { console.log("do A"); }                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                    


class B extends A {
    do() { console.log("do B"); }

    doB() { console.log("what"); }
}


var a = new A();
var b = new B();

a.do();
b.do();
b.doB();

console.log(a instanceof A)  // true
console.log(a instanceof B)  // false

console.log(b instanceof A)  // true
console.log(b instanceof B)  // true


class Base {
    doBase() { console.log("this is the base!"); }
}


// now add Base to B's chain, so that B -> A -> Base
// TODO: doesn't work!

Object.setPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(B)), Base.prototype)
//Object.setPrototypeOf(Object.getPrototypeOf(B), Base)


console.log(Object.getPrototypeOf(B))
console.log(Object.getPrototypeOf(Object.getPrototypeOf(B)))


var c = new B();

console.log(c instanceof B)    // true
console.log(c instanceof A)    // true
console.log(c instanceof Base) // false (!!! how to fix it?)

c.doBase();                    // crash, not a function

推荐答案

这显示了从B到A的继承关系;

This shows inheritance relationship from B to A;

console.log(Object.getPrototypeOf(B.prototype) === A.prototype);

因此,鉴于此;

class A { do() { console.log("do A"); } }

class B extends A {
  do() { console.log("do B"); }
  doB() { console.log("what"); }
}

class Base {
  doBase() { console.log("this is the base!"); }
}

没有引用A,您需要这个;

Without referencing A, you need this;

Object.setPrototypeOf(Object.getPrototypeOf(B.prototype), Base.prototype);

那样;

console.log(new A() instanceof Base); // true
console.log(new B() instanceof Base); // true
(new B()).doBase(); // this is the base!

那怎么样?在此处播放.

如Bergi所述,以与类扩展相同的方式完成Object.setPrototypeOf(A,Base)而不引用A,Object.setPrototypeOf(Object.getPrototypeOf(B.prototype.constructor), Base). (需要对此进行调查,报告它会导致循环继承错误...)

As mentioned by Bergi, to accomplish Object.setPrototypeOf(A,Base) in the same manner as class extension without referencing A, Object.setPrototypeOf(Object.getPrototypeOf(B.prototype.constructor), Base). (Need to investigate this, reported that it causes a cyclic inheritance error...)

此处扩展了测试,以执行如上所述的类构造函数继承.可在Chrome 53(V8 5.3.332.45)中使用.

Extended the test here to perform the class constructor inheritance as shown above. Works in Chrome 53 (V8 5.3.332.45).

请注意,这是对猴子的继承链进行修补.对于性能而言,这不是一个奇妙的想法.

这篇关于将基类添加到现有的原型链中,以便instanceof起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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