Inheritance with JavaScript

Apr 3, 2017

"But how can I do inheritance with JavaScript?" Following the ECMAScript standard, the notation someObject.[[Prototype]] is used to designate the prototype of someObject.

var KlasaA = function() {
   this.ime = "Klasa A";
}
console.log(KlasaA);

var a = new KlasaA();
KlasaA.prototype.prikazi = function() {
   console.log(this.ime);
}
a.prikazi();

var inheritsFrom = function(child, parent) {
   child.prototype = Object.create(parent.prototype);
};

var KlasaB = function() {
   this.ime = "Klasa B";
   this.staime = "I'm dete od A";
}

  inheritsFrom(KlasaB, KlasaA);
  
  var b = new KlasaB();

KlasaB.prototype.prikazi = function() {
  KlasaA.prototype.prikazi.call(this);
  console.log(this.staime);
}
  b.prikazi();
  
var KlasaC = function() {
   this.ime = "Klasa C";
   this.staime = "I'm drugo dete od B";
}
   inheritsFrom(KlasaC, KlasaB);
KlasaC.prototype.prikazi = function() {
  KlasaB.prototype.prikazi.call(this);
  console.log("Ma, radi!");
}
  var c = new KlasaC();
  c.prikazi();