0

I need to execute all methods in random order from main_method (i will create a class instance later).
But when first function start executing it shows that "this" inside her is equal undefined.
How can i fix this? Thx

class test {
 construstor(smth) {
  this.smth = smth
 }
 async main_method {
  for (let func of [this.#method1, this.#method2, this.#method3].sort(() => Math.random() - 0.5 )) {
    await func(this.smth);
  }
 }
 async #method1(smth) {
 // this === undefined
  ///
 }
 async #method2(smth) {
  // this === undefined
  ///
 }
 async #method3(smth) {
  // this === undefined
  ///
 }
}
4
  • 1
    await func.call(this, this.smth) Commented Jul 13, 2022 at 22:20
  • or var self = this and after that func(self.smth) Commented Jul 13, 2022 at 22:21
  • That won't help, @IT The issue isn't that the parameter smth is undefined, it's this. Your version still invokes func with no context Commented Jul 13, 2022 at 22:24
  • Do not use JavaScript Array .sort() method for shuffling! Commented Jul 13, 2022 at 22:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.