I was also interested on how to inherit in prototypical way from ES6 class, just to understand more in JS and here what I can propose:
class Parent {
constructor(data){
this.#setPrivateProperty(data);
}
#privateProperty = "Parent private property";
#setPrivateProperty = (data)=>{
this.#privateProperty = data;
}
parentPublicMethod = ()=>{
console.log("Parent public method responded:", this.#privateProperty);
}
}
function Child(data, parentData){
this.__proto__ = new Parent(parentData)
this.childPublicProperty = data;
}
Child.prototype = Parent.prototype;
Child.prototype.constructor = Child;
let c = new Child("Child data", "Parent data");
// Output: "Parent public method responded: Parent data"
c.parentPublicMethod();
// Output: "Child public property value is: Child data"
console.log("Child public property value is:", c.childPublicProperty);
// Output: "1. Instance of Child: true 2. Instance of Parent: true"
console.log("1. Instance of Child:", c instanceof Child, "2. Instance of Parent:", c instanceof Parent);
I will be very grateful to those people, who will criticize this code sample and maybe we get some more details. Thanks to All in advance!!!