S
S
Sergey Erzhovich2022-02-20 23:07:01
OOP
Sergey Erzhovich, 2022-02-20 23:07:01

Why is the context lost when passing a class to another class?

In the last console log, the prop disappears somewhere.

class Stack {
  lols = [];
  addLol(lol) {
    this.lols.push(lol);
  }
}

const stack = new Stack();

abstract class BaseLol {
  constructor(...prop) {
    stack.addLol(this);
    this.init(...prop);
    console.log(stack); //Stack { lols: [ Lol { prop: 'qwe' } ] }
  }
  abstract init(...prop);
}

class Lol extends BaseLol {
  prop;
  init(prop) {
    console.log(prop); //qwe
    this.prop = prop;
  }
}

new Lol("qwe");

console.log(stack); //Stack { lols: [ Lol { prop: undefined } ] }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Сергей Ержович, 2022-02-21
@ Drilled-prog

В общем проблема была в tsconfig, стоял "target": "ESNext", поменял на ES6 стало норм

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question