JavaScript 6 03: Inheritance




In this video we go over how to inherit from classes (polymorphism) and we also go over how to override methods and call base methods.

Twitter ► https://twitter.com/ShieldCrush
Instagram ► https://www.instagram.com/BrentFarris

Original source


9 responses to “JavaScript 6 03: Inheritance”

  1. You shouldn't call super.constructor, you need to simply call super.

    class Person {
    constructor(name, age) {
    super(name)
    this.age = age
    }
    }

    Also some advice for your coding style: stop using semi colons

  2. Thanks Brent 🙂
    Could you please explain why I'm getting this error :
    VM656:3 Uncaught ReferenceError: this is not defined(…)SpecialCrazyDev @ VM656:3(anonymous function) @ VM822:1InjectedScript._evaluateOn @ VM103:145InjectedScript._evaluateAndWrap @ VM103:137InjectedScript.evaluate @ VM103:118
    var specialDevvv = new SpecialCrazyDev("UI dev",20);

    This is my code :
    class CrazyDev{

    constructor(job){
    this.job = job;
    }
    toString(){
    return "My job is " + this.job;
    }
    }

    class SpecialCrazyDev extends CrazyDev{
    constructor(job,age){
    super.constructor(job);
    this.age=age;
    }

    toString(){
    return super.toString() + "I am a special crazy dev, I'm " + this.age+" years old !!";
    }
    }

  3. class human {
    contructor(name){
    this.nm = name;
    }

    toString(){
    return "Hello, my name is" + this.name + ".";
    }
    }
    class enfant extends human{constructor(name, age){super.constructor(name); this.ag = age;} toString(){ return super.toString() + "im i human im enfant and my age is = "+ this.ag ; } }
    undefined
    var lolo = new enfant("mopi", 45);
    ReferenceError: |this| used uninitialized in enfant class constructor

  4. Nice video, thanks!!

    When I tried your example… i get |this| not initialized in class constructor error, for the creation of Person object.

    It worked after changing it to super(name); instead of super.constructor(name).
    Do you know what this might be… I'm sure that I have latest firefox nightly build

Leave a Reply