0

I'm doing some JS refresher on inheritance. Say initially I have this:

    const a = function(x) {
      this.x = x;
    };
    const b = function(x, y) {
      this.y = y;
    };

In the end if I have as below, that's I will get:

    const newB = new b('x', 'y');
    newB.getX(); // x
    newB.getY(); // y

How do I go about this?

rory-h
  • 660
  • 1
  • 12
  • 30
  • 1
    In ES2015 ("ES6") and above: `class a { constructor(x) { this.x = x; } } class b extends a { constructor(x, y) { super(x); this.y = y; } }` (Plus the `getX` and `getY` methods.) Details in the linked question's answers. – T.J. Crowder Dec 17 '18 at 13:29
  • Side note: You can do what you like in your own code, of course, but the *overwhelming* convention in JavaScript is that constructor function names have an initial capital letter (so, `A` and `B`, rather than `a` and `b`). Sticking to conventions when asking for help is best, as it ensures the question is clear to people. – T.J. Crowder Dec 17 '18 at 13:31

0 Answers0