Skip to content
Advertisement

push array in class is undefined

this is summary of my code. Frame is defined as

export type Frame = {
  changes: readonly vscode.TextDocumentContentChangeEvent[];
  selections: readonly vscode.Selection[];
};

record.ts

export default class SomeClass {
  private timeout: NodeJS.Timer | undefined = undefined;
  private _text = Frame[][] = [];

  constructor() {
    // Do some constructor
  }

  private onDidChange() {
    this.triggerSave();
  }

  private triggerSave() {
    if (this.timeout) {
      clearTimeout(this.timeout);
      this.timeout = undefined;
    }

    this.timeout = setTimeout(this.save, 500);
  }

  private save() {
    this._text.push('__someframething__');
    console.log(this._text);
    // Output: undefined
  }
}

on save method, _text must be Frame Thing

But on the console, it’s undefined. what’s wrong with it?

Full code on my gist record.ts buffer.ts

Advertisement

Answer

try:

this.save = this.save.bind(this) in your constructor.

greetings

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement