DEV Community

Calin Baenen
Calin Baenen

Posted on

Is there a suitable equivalent to `Function.caller` in modern JS?

So, as MDN (Mozilla Developer Network) says, Function.caller is not only deprecated, but removed from the Relevant Web Standards.
But, this isn't good for me, as I want to know what function is calling a function I have.

I have a set of private variables, and I only want "friend functions" to see those variables without having to return every function from another function. (Also because these private variables are user-defined, and thus can't be predicted, or written in advance.)

So, I was wondering, does modern JS provide anything similar to Function.caller?

Thanks!
Cheers!

Top comments (6)

Collapse
 
akashkava profile image
Akash Kava

Not possible but you can use symbols to store private data and expose those symbols to friend functions.

Collapse
 
calinzbaenen profile image
Calin Baenen

That's the approach I'm going for now, but it would have been nice if Function.prototype.caller still existed, because then I could just make a getter function for each key, and call it a day.
Sadly, symbols can still be exposed, unless you want to work with Proxys.

Collapse
 
georgecoldham profile image
George

A sadly irritating workaround :/

Collapse
 
calinzbaenen profile image
Calin Baenen

I know. - Also dangerous because it's not completely private.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Sounds a bit like an XY Problem

Collapse
 
calinzbaenen profile image
Calin Baenen

XY?