Pull shared code out of Jovo3 handler functions into subroutines?


#1

Quick (I hope) question:

Like the example code I based them on, many of my Jovo3 Intent Handler routines end in essentially the same boilerplate:

  if(this.isAlexaSkill()) {
    // Do things to ask Alexa to play the mp3
  } else if(this.isGoogleAction())
    // Do things to ask Google Home to play the mp3
  }

I’d really like to factor that out into a subroutine, rather than it being a copy-paste block – easier to read, less fragile, etcetera.

Problem: I’m still learning Javascript/Typescript as I go, and my attempts to turn this into a subroutine have run into scoping issues. In some attempts, the references to this seem to mostly be satisfied but it seems to be unhappy about having parameters. In others, Typescript complains about not knowing the type of this and so being unable to invoke methods on that object. And of course if i put it outside the setHandler({}), it doesn’t have this in scope.

Is there a clean way to move the shared logic into a shared function? Or should I just leave the copypasta as it stands?