Skip to main content

Inheritance with constructor function in javascript

Object literal like

var derived = {
    a: 10
};
be easily extended using
derived.__proto__ = base;
where base is

var base = {
    x : 5
};
But, what if we using constructor function?
We need to do like this

var Person = function (name) {
    this.name = name;
}
Person.prototype.sayHello = function () {
    console.log('Hello, I am '+this.name);
}
Person.prototype.getName = function () {
    return this.name;
}
var per = new Person('Manan');
per.sayHello(); //prints --> Hello, I am Manan

var Employee = function (name, company) {
    Person.call(this, name);
    this.company = company;
}
Employee.prototype.sayHello = function () {
    console.log('Hello, I am '+this.getName()+' and I am working at '+this.company);
}
Employee.prototype.__proto__ = Person.prototype;
var emp = new Employee('Manan', 'Integ');
emp.sayHello(); //prints --> Hello, I am Manan and I am working at Integ

Notice that we are calling Person function from Employee constructor function using Employee's this. This way whatever initializing properties of Person will be initialized and assigned to Employee's this. So Whatever properties should be in Person's object we have it in Employee's object.

Now, properties assigned to Person.prototype we need to get that too. To do so we assigned Person.prototype to Employee.prototype.__proto__. This creates prototype chain of emp object like below and emp object can access properties of Person.prototype though the chain.

emp
    |----   __proto__   ===  Employee.prototype
                            |----  __proto__   ===  Person.prototype

Comments

Popular posts from this blog

Basics of quantum computing: change of basis

Computing requires classical bits, which can hold a value of 0 or 1. Quantum computing gets benefit from superposition of quantum bit (qubit). Qubit is a representation of state of quantum object (electron, proton, photon, etc). The qubit holds a value between 0 and 1 while it is in superposition state. Superposition means the qubit possesses multiple values at the same time so we can harness its power for parallel computing. Once we try to measure the value it can be collapsed into either 0 or 1 based on probability. (Observer collapses wave function simply by observing.) Using bloch sphere, we can represent qubit physically. (The bloch sphere is a physical model to represent a spin state of qubit) A state vector can point to any direction from the center of the sphere. If a vector points to Z+ and Z-, it represents 0 and 1 state respectively. All other vectors represent superposition state in a Z basis. Now, if a vector points to X+, X-, Y+, Y-, all are at a 90-degree angle from ...

synchronized, wait() and notify() in java

synchronized keyword in java used to make sure that the code block would be executed by one and only one thread at a time. Using the keyword one can write a code block which would be side effects free from other concurrently running thread. There are two way we can use the keyword: 1) synchronized block and 2) synchronized method. synchronized (lockingObject) {     // code which must be executed by single thread at a time. } public synchronized void doWork () {     // code which must be executed by single thread at a time. } The synchronized block locks on lockingObject whereas synchronized method locks on object of that method ( this ). Good example of this is to achieve singleton pattern. public class Singleton {     private static Singleton s;     public static Singleton getInstance () {         if ( s == null ) {             synchronized (Singleton.class) {    ...

Generics wild card in java

List<Double> or List<Float> cannot be assigned to List<Number> because, List<Double> isn't extend List<Number>. List<Double> dblList = new ArrayList<>(); List<Number> numList = dblList; //illegal To do that we need to use upper-bound wild card. List<Double> dblList = new ArrayList<>(); List<? extends Number> numList = dblList; Same way List<Number> isn't extend List<Object> so, List<Object> cannot be assigned to List<Number> List<Object> objList = new ArrayList<>(); List<Number> numList = objList; //illegal To do that we need to use lower-bound wild card List<Object> objList = new ArrayList<>(); List<? super Number> numList = objList; Upper-bound We can add object of Double type or its sub types to List<Double>. Same way we can add Number or its sub types (e.g. Double, Float, Integer) to List<Number> but, we cannot assign dblLis...