Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the calling Class in JavaScript [duplicate]

Tags:

javascript

Is it possible to get the class calling a function in JavaScript?

For example:

function Foo(){
this.alertCaller = alertCaller;
}
function Bar(){
this.alertCaller = alertCaller;
}

function alertCaller(){
    alert(*calling object*);
}

When calling the Foo().alertCaller() i want to output the class Foo() and when calling Bar().alertCaller() I want to outbut Bar(). Is there any way I can do this?

like image 245
Ood Avatar asked Jan 28 '26 11:01

Ood


1 Answers

Try this :

function alertCaller(){
    alert(this.constructor);
}
like image 142
Serge K. Avatar answered Jan 31 '26 02:01

Serge K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!