Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototypical inheritance without prototype?

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)

like image 817
ankush981 Avatar asked Jul 03 '15 09:07

ankush981


1 Answers

new Manager() instanceof Manager
> true
new Manager() instanceof Employee
> false
like image 64
Alan Tam Avatar answered Oct 10 '22 07:10

Alan Tam