Tuesday, July 3, 2018

Javascript inheritance



Prototypical  Inheritance


Code Example:

function demo(a){
this.a=a;
}

function sample(){
demo.call(this,20);
this.b=10;
}

var x = new sample();

console.log(x.a)


Classical Inheritance:

Code Example:
function demo(){
this.a=10;
}

function sample(){
this.x=demo();
this.x();
this.b=10;
}

var x = new sample();

console.log(x.a)

Prototype Inheritance

Code Example:
function demo(){
this.a=20;
}

function sample(){
this.b=10;
}

sample.prototype=new demo();

var x = new sample();
console.log(x.a)

No comments:

Post a Comment

function declaration, expression and call/invoke/execution

  The  function  declaration defines a function with the specified parameters. The  function   keyword can be used to define a function ins...