Search code examples
javascriptinheritanceprototype

JavaScript Prototype property not added to its inherited objects


I am following MDN guide about adding property and this is what my code looks like

'use strict';

function Employee() {
    this.name = '';
    this.dept = 'general';
}
Employee.prototype.specialty = 'none';

function Manager() {
    Employee.call(this);
    this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;

function WorkerBee() {
    Employee.call(this);
    this.projects = [];
}
WorkerBee.prototype = Object.create(Employee.prototype);
WorkerBee.prototype.constructor = WorkerBee;

function SalesPerson() {
    WorkerBee.call(this);
    this.dept = 'sales';
    this.quota = 10;
}
SalesPerson.prototype = Object.create(WorkerBee.prototype);
SalesPerson.prototype.constructor = SalesPerson;

function Engineer() {
    WorkerBee.call(this);
    this.dept = 'engineering';
    this.machine = '';
}
Engineer.prototype = Object.create(WorkerBee.prototype);
Engineer.prototype.constructor = Engineer;

let mark = new WorkerBee;
console.log(mark);

mark.name = 'Doe, Mark';
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);

mark.bonus = 3000;
console.log(mark);

When I run this, I do not see specialty property for mark object.

WorkerBee { name: '', dept: 'general', projects: [] }
WorkerBee { name: 'Doe, Mark', dept: 'admin', projects: [ 'navigator' ] }
WorkerBee {
  name: 'Doe, Mark',
  dept: 'admin',
  projects: [ 'navigator' ],
  bonus: 3000 }

What am I missing?

Thanks


UPDATE

As per @adiga answer, I was able to locate the specialty property in the prototype chain.

enter image description here


Solution

  • The same reason you won't be able to see toString or hasOwnProperty properties even if you are able to call them from mark object. specialty is not an own property of the mark object but it is inherited.

    The browser's console displays only the own properties of an object. Expand the __proto__ of mark object and you will be able to see the inherited properties.