we use super to reference the parent prototype's constructor function
class Resource { constructor() { } }
class Widget extends Resource {
constructor() {
super();
if (super.constructor === Resource.prototype.constructor) {
console.log("It's true!");
}
}
}
this
this
once you get to the highest prototype in the chain: Object
this
, and if there is no parent constructor, you will have no this
value and it will stay undefined
constructor(...args) {
super(...args);
}
JS will switch the current context for this
for you, even if it's in the parent class!
class Parent {
constructor() { }
whoami() {
console.log(this); // will print out the current SUBCLASS object
}
}
class Child extends Parent {
constructor() { super(); }
whoami() {
console.log(this); // will print out the current Child object
super.whoami();
}
}
let w = new Child();
w.whoami();
So you can do this instead:
class Bee {
constructor() { }
whoami() {
console.log(this); // will print out the current SUBCLASS object
}
}
class WorkerBee extends Bee {
// constructor() { super(); }
}
let w = new WorkerBee();
console.log(w.whoami());
Function.prototype.call()
this
value and arguments provided individually