ある object
に対して object instanceof Class_
が true
を返すのは
- object.constructor.prototype が Class_.prototype
- object.constructor.prototype.constructor.prototype が Class_.prototype
- object.constructor.prototype.constructor.prototype.consrtuctor.prototype が Class_.prototype
- 以下繰り返し
な時。なので当然の如く
function Class_() {}
object = new Class_
object.constructor === Class_ // true
object instanceof Class_ // true
また最も単純なプロトタイプ拡張だと思われる
function Super() {}
function Child() {}
Child.prototype = new Super
c = new Child
c instanceof Super // true !!!
が成り立つし
function ClassA() {}
function ClassB() {}
ClassA.prototype = ClassB.prototype
cA = new ClassA
cB = new ClassB
cA instanceof ClassB // true !!!
cB instanceof ClassA // true !!!
という2つの異なるコンストラクタから作られたインスタンスが互いのインスタンスである、という謎の状況も作れる。
ちなみに、後付することはできないので注意が必要
function Super() {}
function Child() {}
c = new Child
c.constructor.prototype = Super.prototype
c instanceof Super // false
c2 = new Child
c2 instanceof Super // true !!!