0

I have a rudimentary keydown listener.

private _foo(event: KeyboardEvent) {
   /// 
}

<div @keydown=${this._foo}>test</div>

If I want to pass a function into that, why can't I just add it as an argument like:

<div @keydown=${this._foo(myOtherFunc)}>test</div>

instead, I need to do the following as an anonymous function but I don't understand why?

private _foo(event: KeyboardEvent, myOtherFunc: Function) {
   
   /// 
}

<div @keydown=${(event: KeyboardEvent) => this._foo(event, myOtherFunc)}>test</div>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
sal3jfc
  • 517
  • 4
  • 14
  • 2
    `keyDown=${this._foo(...)}` is just invoking `this.foo` and setting its result to the `keyDown` handler. The second one is making a new function that will invoke `this._foo` later when it's invoked. If you wan to use `this.foo(...)` in that manner, it should return an event handling function. – CollinD Sep 28 '22 at 15:56
  • ah yes, it's invoking it! thank you! – sal3jfc Sep 28 '22 at 15:57
  • 1
    Does this answer your question? [Pass an extra argument to a callback function](https://stackoverflow.com/questions/40802071/pass-an-extra-argument-to-a-callback-function) – Heretic Monkey Sep 28 '22 at 16:01

0 Answers0