D
D
disco_dinosaur2018-11-13 03:40:15
JavaScript
disco_dinosaur, 2018-11-13 03:40:15

Inheritance chain how?

Hey! Can you please tell me how to implement type inheritance from SomePencil to AnotherPencil? Thanks

class Pencil {
  constructor(color) {
    this.color = color;
  }
  intro() {
    console.log(`this is ${this.color} pencil`);
  }
};

class SomePencil extends Pencil {
  constructor(color, type) {
    super(color);
    this.type = type;
  }
};
class AnotherPencil extends SomePencil {
  constructor(color,) {
    super(color);
  }
};

let pen1 = new Pencil();
let pen2 = new SomePencil("red", "common");
let pen3 = new AnotherPencil("green");

console.log("type" in pen3); // true

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-11-13
@Arik

class AnotherPencil extends SomePencil {
    constructor(color, type) {
        super(color, type);
    }
};

or not to declare a constructor at all, because don't add anything new

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question