1

I've been doing some experiments about high-order functions and decided to understand how to make an custom addEventListener function so to understand what's under the hood.

enter image description here

As you can see, it requires an 'event' parameter, but even though I'm not supplying at the second function, it works..

But here:

enter image description here

Yes, I get it, in vanilla JS the second function also works and coordinates gets undefined, but in this TypeScript code, first one works, second not.. I'm just confused.

Also, what's the right way to make an function's parameters optional, either insert params or empty.

Thanks for your time.

Edit: I think I'm looking for some kind of method overload like in java

public void someFunc(int a, int b)
public void someFunc(int a)
public void someFunc()

What's the equilavent of this in Js?

  • [What exactly is the parameter e (event) and why pass it to JavaScript functions?](https://stackoverflow.com/questions/35936365/what-exactly-is-the-parameter-e-event-and-why-pass-it-to-javascript-functions) – Andreas Mar 27 '21 at 08:57
  • Can you please update these images with [`Code Snippets`](https://meta.stackoverflow.com/a/356679/13829657)? – Muhammed Bera Koç Mar 27 '21 at 09:23
  • customAddEventListener requires a function that can be called with a single Event. JavaScript doesn't care about arity, so a function that declares no parameters meets that requirement. By contrast your second function requires three numbers, then you're not passing any. – jonrsharpe Mar 27 '21 at 10:07
  • [Please consider replacing the images of code/errors with text](//meta.stackoverflow.com/q/285551) so that it constitutes a [mcve] which, when dropped into a standalone IDE like [The TypeScript Playground](https://www.typescriptlang.org/play), demonstrates the issue you are facing. This would greatly improve the chance that any answer you receive will be directly applicable to your question. – jcalz Mar 28 '21 at 01:54
  • 1
    There is a TypeScript FAQ entry [Why are functions with fewer parameters assignable to functions that take more parameters?](//github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters) that explains your first image. The difference between your two images is about *implementing* a function versus *calling* it. You can't call a function without its required inputs, but you can certainly implement one that ignores those inputs. I'm happy to write up an answer if you edit the question to include code as text. – jcalz Mar 28 '21 at 01:57
  • @jcalz plese make an answer so I can accept your answer, so this thread can marked as checked, also thanks everybody, you really helped, also I will add code-snippets rather than images next time. – ilkerrilker Mar 28 '21 at 09:09
  • Sure, I will write up an answer if you edit the code here to use text instead of (or in addition to) images, so I can use that code in my answer. – jcalz Mar 28 '21 at 14:00

0 Answers0