Self service function

In JavaScript, functions are just objects that can be called. So, what is the easiest way for a function body to refer to the actual function object?

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

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

But is there an easier way? I suspect not, but I could be wrong.

+7
javascript
source share
5 answers

I cannot imagine a case where a named functional expression cannot replace an anonymous function expression. Therefore, I would suggest calling a function if you are going to call it from the inside (i.e. if you are going to use recursion):

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

This works even if it is a link:

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

If you don't want to pollute the namespace, you can even use recursive IIFE (immediately called function expression):

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

Also, something like this:

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

Also works and will not pollute the namespace.

+12
source share

This is actually a frustration because you used the ability to use arguments. callee that could get a link to a function. As already mentioned, this is no longer possible.

A pattern appears that arises for this ... and you need to name it as a function and 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 a named function and do your business. Note that you need to use the .call syntax if you want to name it as a method (so this makes sense), but other than that it's pretty simple.

Another option is to make a truly functional approach in which you pass the object that will be used for the 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 can be argued that it is better and more flexible. I think this is a matter of taste, really.

+4
source share

arguments.callee inside the function will be a link to the function itself, but this is not a good form. You must name the function, then you can just use the name inside the function

+2
source share

I would give your functions a name and then use it for reference. If you have a declared function or function variable, it already has a name.

 function test1(){ // test1(); } var test2 = function(){ // test2(); }; 

If you have an anonymous function, you can give it a name. This name will exist only inside the function.

 someFunc(function test3(){ // test3(); }); // test3 will be undefined here 
+2
source share

You can, but since the function is anonymous, a fictitious way of identifying it must be adopted. You can learn a lot about how to do this by simply looking at this great article: Anonymous JavaScript Recursion

0
source share

All Articles