Problem with Multi-Level inheritance

Posted on
  • class A {
      constructor() {}
    }
    
    class B extends A {
      constructor() {
        super();
      }
    }
    
    class C extends B {
      constructor() {
        super();
      }
    }
    
    function onInit() {
      const c = new C();
    }
    

    Hi, I have a problem, which could be boiled down to the code snippet given above.
    If I run it on my Espruino WiFi (2v16), I get a recursion error, since if class B tries to call the constructor of A, it calls its own constructor.
    Does anyone has a workaround for this problem? I already tried passing different argument lists into the constructors to make them distinguishable (in C++ mapping terms at least). Without any luck. I can easily instantiate B or A, but not C.
    I would really appreaciate your help. Thank you very much.

  • This does work, but I really would like to use class notation.

    var A = function() {};
    
    var B = function() {
      A.call(this);
    };
    B.prototype = Object.create(A.prototype);
    B.prototype.constructor = B;
    
    var C = function() {
      B.call(this);
    };
    
    C.prototype = Object.create(B.prototype);
    C.prototype.constructor = C;
    
    function onInit() {
      const c = new C();
    }
    
  • This was actually fixed just last week! https://github.com/espruino/Espruino/com­mit/435b4dadecba23aa256e51ea5985623c1fd8­b65f

    If you install a 'cutting edge' build it will hopefully be fixed!

  • Works like a charm. Thanks Gordon. What a coincidence ;)

  • :) Yes! It's odd - someone else picked up on it last week which is why I fixed it, but otherwise the bug report itself is over 4 years old!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Problem with Multi-Level inheritance

Posted by Avatar for llakie @llakie

Actions