0

I'm not sure this kind of feature is available in Javascript. Here is what I wanted to archive.

Let's say we have an object instance called obj and I want to call any function in this object instance but I'm not defining the function name at the beginning.

const obj = {};

obj.functionOne({id: 1}); // execution 1
obj.functionTwo({id: 1}); // execution 2

I wanted to execute one single function (_default) for both execution 1 and execution 2. withing that function I need to know what is the function name actually executed.

eg:


obj = {

  _default: (params) {
    console.log('Function Name is', Function.name); // will be print `functionOne` or `functionTwo`
    console.log(params); // will be print {id: 1} for both cases.
  }

}

Can someone give me some insight on how to archive this kind of requirement?

Thanks in advance.

Viran Malaka
  • 417
  • 4
  • 10
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy | [In the Proxy handler, how to distiguish getting a property (var) vs calling a method?](https://stackoverflow.com/q/50027225) – VLAZ Jan 08 '22 at 10:29
  • 2
    This could be XY problem. Please, explain the problem you try to solve. Function.name alone is not enough because it's impractical and shouldn't be relied on. Possible solutions could be very different depending on the task – Estus Flask Jan 08 '22 at 10:36

0 Answers0