-
|
First of all, polytype is awesome! Not sure if I'm facing a bug or just don't understand something. It's about accessing base classes properties. Docs says: If different base classes include a member with the same name, the syntax Now, I have situation like this: import { classes } from 'polytype';
const getId = () => {
return String(Math.random().toFixed(3).replace('.', ''));
};
class WithId {
id = getId();
constructor() {
console.log(111, 'WithId id', this.id);
}
}
class A extends classes(WithId) {
constructor() {
super();
console.log(111, 'A id', this.id);
}
}
class B extends classes(WithId) {
constructor() {
super();
console.log(111, 'B id', this.id);
}
}
class C extends classes(A, B) {
constructor() {
super();
console.log(111, 'C id', super.class(A).id, this.class(A).id, this.id);
}
}
const c = new C();I expect that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @lindi0v, thanks for the appreciation. The behavior you observe is expected since the You can compare your code to the simplified example below, where class 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();When I run this locally, it prints something like: WithId id 0107
C id undefined 0107Note in the last line that |
Beta Was this translation helpful? Give feedback.
Hi @lindi0v, thanks for the appreciation. The behavior you observe is expected since the
superkeyword can only access properties on an object's prototype, but in your case, theidproperty is always set on the new object itself withid = getId();.You can compare your code to the simplified example below, where class
Cinherits directly fromWidthId, without using Polytype.W…