cachaito
2/4/2019 - 11:17 PM

ES6 class extends

Kiedy rozszerzamy klasę, trzeba zawsze użyć "super()" - inaczej nie będziemy mieli dostępu do "this" w dziedziczonej klasie!

class MyClass {
    constructor() {
      this.myProp = 4;
      console.log(`THIS from MyClass ${this}`); // myProp: 4
    }
}

class OtherClass extends MyClass {
    constructor() {
      super(); // required!
      console.log(`THIS from OtherClass ${this}`); // myProp: 4
    }
}

var final = new OtherClass();

function Animal(name, age) {
    this.name = name;
    this.age = age;

    this.rap = function() { //private
        return this.name + 'rapping';
    }
}

Animal.prototype.speak = function() {
   return this.name + 'said ugh!';
}

function Gorilla(name, age) {
    Animal.call(this, name, age);

    this.sing = function() {
        return this.speak() + ' ulalalalaal';
    }
    
    this.hide = function() {
        return Animal.prototype.speak.call(this, name) + 'um um um';
    }
}

Gorilla.prototype = Object.create(Animal.prototype);

var pawian = new Gorilla('Lutek', 34);