Inheritence in JavaScript

 
function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.toString = function() {
    console.log("I, " + this.name + ", am " + this.age + " years.");
};

function Student(name, age, isCampus) {
    this.name = name;
    this.age = age;
    this.isCampus = isCampus;
}
Student.prototype.toString = function() {
  console.log("I, " + this.name + ", studies " + (this.isCampus ? "at campus" : "remotely") + " and am " + this.age + " years.");
};

var person1 = new Person("Joe", 42);
var student1 = new Student("Jen", 22);

person1.toString(); // "I, Joe, am 42 years."
student1.toString(); // "I, Jen, studies remotely and am 22 years."

In the code above we got duplicate properties of name and age for both person and student.
To make this more effieicent we can use inheritence if we know that student1 is person1 and have the same name and age..

 
function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.toString = function() {
    console.log("I, " + this.name + ", am " + this.age + " years.");
};

function Student(name, age, isCampus) {
    Person.call(this, name, age); //this = student
    //by using call we can control what object gets sent in
    this.isCampus = isCampus;
}

//Because we also want the protype functionality and the functions there we create a prototype object out of the persons prototype

Student.prototype = Object.create(Person.prototype);

//The issue though is that if we ask what type Student is now it will say its a Person instead of Studen
//To combat this we set the prototypes constructor to Student:
Student.prototype.constructor = Student;



Student.prototype.toString = function() {
  
  console.log("I, " + this.name + ", studies " + (this.isCampus ? "at campus" : "remotely") + " and am " + this.age + " years.");
  
};


var person1 = new Person("Joe", 42);
var student1 = new Student("Joe", 42);

person1.toString(); // "I, Joe, am 42 years."
student1.toString(); // "I, Joe, studies remotely and am 42 years."

Check of which type an object is

by adding

 
console.log(person1 instanceof Person); // True
console.log(person1 instanceof Student); // False

console.log(student1 instanceof Person); // True
console.log(student1 instanceof Student); // True

With the instanceof operator we can check if a constructor been made to create an object.
As you see student1 is both a instanceof Person and Student!

All objects will return true for instanceof Object.

 
function Address(addressLine, postalCode, city) {
	this.addressLine = addressLine;
    this.postalCode = postalCode;
    this.city = city;
}

var address1 = new Address("4 shangri road", "N51JT", "London"); // OK address!
var address2 = new Address("3 sherry road", "no idea", "Edinburgh");

The problem here is like address2 that its allowed to put in whatever into postalCode.
To validate postalCode we will make it private with use of getters and setters.

 
function Address(addressLine, postalCode, city) {
	this.addressLine = addressLine;
    this.city = city;
    
    Object.defineProperty(this, "postalCode",
    {
     get: function(){
       return postalCode;
     },
      
     set: function(value) {
       if (!/^[1-9]\d{2} ?\d{2}$/.test(value)){
          throw new Error("postalCode seems to beinvalid!");
       }
       postalCode = value;
     }
      
    });
  this.postalCode = postalCode;
}

var address1 = new Address("4 shangri road", "756 53", "London"); // OK address!
var address2 = new Address("3 sherry road", "no idea", "Edinburgh");

Add your comment