I think I understand prototypical inheritance in JS but am having trouble writing code to demonstrate a particular idea I have. Consider this extremely simple scenario, where Manager objects derive from Employee objects:
function Employee()
{
this.name = "Axel";
this.dept = "R&D";
}
function Manager()
{
Employee.call(this);
this.reports = ["Report 1", "Report 2", "Report 3"];
}
console.log(new Manager());
The output is:
Manager {name: "Axel", dept: "R&D", reports: Array[3]}
Oddly enough, it seems to me that we've succeeded in demonstrating prototypical inheritance. But the facts that we didn't use prototype
anywhere disturbs me. Surely the code above isn't the way to do it?
Can someone provide an example that shows the above approach failing?
(By the way, the example comes from the official Mozilla docs, minus the setting of prototypes: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model)
new Manager() instanceof Manager
> true
new Manager() instanceof Employee
> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With