Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get function self reference from within that function?

Tags:

javascript

is there any way to get function reference when i'm inside function?

var arrayOfFunction = [ myFunction ];

arrayOfFunction[0]();

function myFunction() {
    removeFromArray( thisFunctionReference ) // how to get function self reference here?
};

function removeFromArray(functionRef) {
    index = arrayOfFunction.indexOf(functionRef);
    if (index > -1) {
        arrayOfFunction.splice(index, 1);
    }
}
like image 767
IT Man Avatar asked Feb 25 '26 23:02

IT Man


1 Answers

In JavaScript, functions are first class members and thus behave like any other object. You can just refer to it by name like this:

myFunction.property = 5;

function myFunction() {
  console.log(myFunction.property) //logs 5
  console.log(myFunction) // logs the function 
};

myFunction();
like image 155
Willem van der Veen Avatar answered Feb 27 '26 12:02

Willem van der Veen



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!