Skip to content

Inheriting from multiple base classes clarification #18

Answered by fasttime
lindi0v asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @lindi0v, thanks for the appreciation. The behavior you observe is expected since the super keyword can only access properties on an object's prototype, but in your case, the id property is always set on the new object itself with id = getId();.

You can compare your code to the simplified example below, where class C inherits directly from WidthId, without using Polytype.

const getId = () => {
  return String(Math.random().toFixed(3).replace('.', ''));
};

class WithId {
    id = getId();
  
    constructor() {
      console.log('WithId id', this.id);
    }
}

class C extends WithId {
  constructor() {
    super();
    console.log('C id', super.id, this.id);
  }
}

const c = new C();

W…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lindi0v
Comment options

Answer selected by lindi0v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants