Given the following case:
class Parent {
propStr = "Hello";
propNum = 42;
constructor(propShared) {
console.log(this.propStr); // Hello
console.log(this.propNum); // 42
console.log(propShared); // w/e
}
}
class Child extends Parent {
propStr = "Hi"; // overridden
propNum = 1337; // overridden
constructor(propShared) {
super(propShared);
}
}
let c = new Child("Foobar");
How do I make sure that the parent properties are properly overridden so that the console.log prints the child's properties?
Since you mentioned typescript, you can use a feature of typescript called Parameter Properties:
class Parent {
constructor(public propStr = "Hello", public propNum = 42, public propShared: any) {
console.log(this.propStr); // Hello
console.log(this.propNum); // 42
console.log(this.propShared); // w/e
}
}
class Child extends Parent {
constructor(propShared: any) {
super("Hi", 1337, propShared);
}
}
let c = new Child("Foobar");
The output will be exactly what you were expecting.