0

I am using Array.find in the following context

module.exports = new class {
   status = { COMMAND_SUCCESS: 1 }

   commands = [{
        name: "history",
        method: this.History
   }];

   commandsNames = ["history"];

   async Execute(command)
   {
        var commandParts = command.toLowerCase().split(" ");
        var commandFound = this.commands.find((command) => { return command.name === commandParts[0] });
        // Removed if commandFound for clarity
        return commandFound["method"]();
   }

   async History()
   {
      // Removed first lines for clarity
      return this.status.COMMAND_SUCCESS; 
      // Cannot read properties of undefined (reading 'COMMAND_SUCCESS')
   }
}

I am tryingh to keep the THIS scope inside my method so i can access the status object. How can i keep the this of my class inside the History method ? My first try was to update my commandFound variable, the original version was

.find(command => command.name === commandParts[0]);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
xxxxooxaw
  • 43
  • 6

0 Answers0