Skip to content Skip to sidebar Skip to footer

Js - 'this' Is Undefined When Calling Method 'indirectly'

My goal is to call a function from a table of functions to generalize command handling (i.e indirectly). Unfortunately, this isundefined when called as such. function Server() {

Solution 1:

The default behavior for this is that it refers to the function scope at the time it's invoked (see below). You can force the value of this by either using bind (MDN) or using arrow function syntax, which lexically-scopes your references to this to wherever you defined the function. This is the change I would make:

"dummy" : server.dummyCommandHandler.bind(this),


Post a Comment for "Js - 'this' Is Undefined When Calling Method 'indirectly'"