0

Is there a way to gracefully handle or intercept all function calls to an Object in JS? Basically in certain cases I want nothing to happen and I don't want JS to throw a "is not a function" exception. So not matter what function I call on the object, for example:

Obj.nonExistentFucntion1();
Obj.nonExistentFucntion2();
Obj.nonExistentFucntion3();

I want no errors to be thrown in the console.

Chirag Arora
  • 877
  • 1
  • 9
  • 22
  • Use `if (obj.nonExistentFucntion) obj.nonExistentFucntion()`? – Bergi Jul 12 '23 at 10:24
  • If you don't want an `if`, you could use proxy to catch the non existing function. – 0stone0 Jul 12 '23 at 10:25
  • How about making use of the [optional chaining operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) which is capable of handling the call operator/`()` optionally as well ... `Obj?.nonExistentFucntion1?.();` – Peter Seliger Aug 27 '23 at 10:04

0 Answers0