Skip to content

Instantly share code, notes, and snippets.

@wentout
Created January 12, 2023 16:48
Show Gist options
  • Save wentout/4793eb85c1a37417650f9edfc59850e7 to your computer and use it in GitHub Desktop.
Save wentout/4793eb85c1a37417650f9edfc59850e7 to your computer and use it in GitHub Desktop.
Ptototype Inheritance just is not the same for Class instances not
class A {
a = 1
constructor() {
console.log('A constructor this.a : ', this.a);
this.a = 1;
console.log('A constructor this.a : ', this.a);
}
}
class B extends A {
a = 2
constructor() {
super();
console.log('B constructor this.a : ', this.a);
this.a = 2;
console.log('B constructor this.a : ', this.a);
}
}
class C extends B {
a = 3
constructor() {
super();
console.log('C constructor this.a : ', this.a);
this.a = 3;
console.log('C constructor this.a : ', this.a);
}
}
console.log('\n\n so here we go with construction : \n');
const instanceC = new C;
console.log('\n\n and now we are checking instance : \n');
console.log('instanceC.a : ', instanceC.a); // 3, exactly as expected
const instanceProtoB = Object.getPrototypeOf(instanceC);
console.log('instanceProtoB.a : ', instanceProtoB.a); // undefined, should be 2
const instanceProtoA = Object.getPrototypeOf(instanceProtoB);
console.log('instanceProtoA.a : ', instanceProtoA.a); // undefined, should be 1
console.log('\n\n then here are instanceof checks : \n');
console.log('instanceC instanceof A : ', instanceC instanceof A);
console.log('instanceC instanceof B : ', instanceC instanceof B);
console.log('instanceC instanceof C : ', instanceC instanceof C);
console.log('\n');
console.log('instanceC_ProtoB instanceof A : ', instanceProtoB instanceof A);
console.log('instanceC_ProtoB instanceof B : ', instanceProtoB instanceof B);
console.log('instanceC_ProtoB instanceof C : ', instanceProtoB instanceof C);
console.log('\n');
console.log('instanceC_ProtoA instanceof A : ', instanceProtoA instanceof A);
console.log('instanceC_ProtoA instanceof B : ', instanceProtoA instanceof B);
console.log('instanceC_ProtoA instanceof C : ', instanceProtoA instanceof C);
console.log('\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment