samedi 7 janvier 2017

prototypal inheritance using constructor function limitation

Not sure this is a good pattern, I uses prototype to solve a problem.

Let's see what is the problem first. The problem is we do not want to alter employee function, we just want to extend it. So I make a new constructor function (softwareEngineer) and used prototype.

var employee = function(name, gender){
  this.name = name;
  this.gender = gender;
}


var softwareEngineer = function(name, gender, java, php){
  employee.call(this, name, gender);
  this.java = java;
  this.php = php
}

softwareEngineer.prototype = new employee();
softwareEngineer1 = new softwareEngineer('alice','female',true, false);

console.log(softwareEngineer1)

http://ift.tt/2i24yI6

But I'm still not confident this is the best pattern for this case, my worry is if the inheritance tree is growing large, it might be hard to trace bug.

Aucun commentaire:

Enregistrer un commentaire