Classes

It is the practice of designing objects to share behaviors and to be able to override shared behaviors with specific ones. Polymorphism takes advantage of inheritance in order to make this happen.

When we copy or merge objects they are not really coped, just referenced in memory.

function mixin( sourceObj, targetObj ) {
  for (var key in sourceObj) {
    // see Kyle book on key value pairs inheritance page 77
  }
}
 
var Person = {
   register: function() {
      console.log('Registering Person');
   }
}
 
var Teacher = function Teacher() {
   this.register = function register() { // notice method exists in Person, if we are inheriting (polymorphism give it the same name)
      Person.register.call(this); // this is how we call the reference
      console.log('Registering Teacher');
   }
} 
 
var Student = function Student() {
   this.register = function register() { // notice method exists in Person, if we are inheriting (polymorphism give it the same name)
      Person.register.call(this); // this is how we call the reference
      console.log('Registering Student');
   }
} 
 
var RegisterStudent = new Student();
RegisterStudent.register(); 
 
var RegisterTeacher = new Teacher();
RegisterTeacher.register();
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License