In the JavaScript MDN guide in the section “Inheritance of properties is inherited”, he explains some of the subtleties of property inheritance.
In particular, I am interested, for example, with an incremental id example:
var idCounter = 1;
function Employee (name, dept) {
this.name = name || "";
this.dept = dept || "general";
this.id = idCounter++;
}
function Manager (name, dept, reports) {...}
Manager.prototype = new Employee;
function WorkerBee (name, dept, projs) {...}
WorkerBee.prototype = new Employee;
function Engineer (name, projs, mach) {...}
Engineer.prototype = new WorkerBee;
function SalesPerson (name, projs, quota) {...}
SalesPerson.prototype = new WorkerBee;
var mac = new Engineer("Wood, Mac");
Basically, it goes on to say that a complete version of this (each constructor has a “base” property, which also calls the constructor above them in the prototype chain) will mean that mac.id will be 5.
I understand that every time an Employee object is created, the id value is incremented by 1, but exactly, when 4 times before creating a "mac" an Employee object is created? I would be grateful if someone could give me a step-by-step process.
edit - , : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#Global_information_in_constructors