Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Self Reference

Tags:

javascript

In JavaScript, functions are simply objects that can be invoked. So what is the easiest way for the body of a function to reference the actual function object?

this can be used to reference the containing object that a function (or more specifically, a method) is called from. But I believe this never refers to the actual function object itself.

Obviously, bind, call, or apply could be used to change the value of this for the function. Or bind could be used to create a version of the function that is always given a reference to itself as its first parameter.

But is there any simpler way? I suspect not, but I could be wrong.

like image 542
Levi Lindsey Avatar asked Oct 09 '14 16:10

Levi Lindsey


People also ask

Can function reference itself?

A function can refer to and call itself.

What is self-reference in mathematics?

Self-reference, the ability of a system to refer to itself, is a necessary condition for complexity, and can indeed form the basis for a definition of complexity.

What is self-reference in communication?

Definition. The self-reference effect refers to people's tendency to better remember information when that information has been linked to the self than when it has not been linked to the self.

What is a self-referential attitude?

A life that is self-referential is one that is flexible, fluid, and creative. Our sense of security comes from a sense of trust in our capacity to deepen it rather than rely exclusively upon the input of other people and institutions.


2 Answers

I cannot think of a case where a named function-expression cannot substitute for an anonymous function-expression. So I would suggest naming the function if you are going to call it from within itself (i.e., if you are going to use recursion):

function myFunc(someArg) {
    ...
    ...
    myFunc(someNewArg);
}

This works even if it is a reference:

var myFunc = function(someArg) {
    ...
}

You can even use a recursive IIFE (immediately-invoked function expression) if you don't want to pollute the namespace:

(function myFunc(arg) {
    ...
    myFunc(someOtherArg);
})(0); //some initial value

Furthermore, doing something like this:

someOtherFunction(function myFunc(arg) {
    ...
    myFunc(otherArg);
});

Also works and won't pollute the namespace.

like image 168
Vivin Paliath Avatar answered Sep 21 '22 15:09

Vivin Paliath


This is actually a frustration of mine, because you used to be able to use arguments.callee which would get you a reference to the function. As someone else mentioned, this is no longer possible.

There is a pattern emerging for this... and that is to name the function yourself as well as assign it. It looks like this:

my_obj.myfunc = function foo(arg1, arg2) {
     // do stuff
     foo.call(this, new_arg1, new_arg2);
}

This way you can reference the named function and go about your business. Note that you do have to use the .call syntax if you want to call it as a method (so that this has meaning) but apart from that, it's pretty straightforward.

Another option is to do a truly functional approach, where you pass the object to be operated on to a function:

function foo(obj, arg1, arg2) {
    // do stuff
    // call recursively:
    foo(obj, new_arg1, new_arg2);
}

// assign as method:
my_obj.foo = function(arg1, arg2) { 
     foo(this, arg1, arg2);
}

It could be argued that this is better and more flexible. I think it's a matter of taste, really.

like image 25
JayKuri Avatar answered Sep 21 '22 15:09

JayKuri