1

I've searched for Recursive Calls in JavaScript, but I want to make a recursive call of a "unnamed" function.

The results I find using Google is something like this

function foo() {
   setTimeout("foo()",0);
}

but I want to make something that would be like:

(function () { alert(this.function) })()

Is this possible?

1
  • Is there some reason you don't want to give your function a name? Commented May 7, 2012 at 16:05

5 Answers 5

5

If you are not in strict mode you can get the function object with arguments.callee, see MDN docs. e.g.

(function () { 
    console.log(typeof arguments.callee); // "function"
    arguments.callee(); // call to itself
})(); 

But as suggested also there, you should avoid this statement and give an identifier to the function, like so

(function foo() { 
    foo(); // call to itself
})(); 
Sign up to request clarification or add additional context in comments.

Comments

0

As far as i know, you can't. You have to have a reference (name or variable) to call it back.

Though there is arguments.callee but it's discouraged

Note: You should avoid using arguments.callee() and just give every function (expression) a name.

Comments

0

You can use a name for the function when passing the function as a value:

setTimeout(function foo() { alert(foo); });

Comments

0

you should never use .callee

you just name the function, which is only available inside the inner scope

setTimeout(function namedFn(x) { 

  // namedFn() exists in here only (see notes below)
  console.log('hello there ' + x);

  if (!x || x < 10) { 
    namedFn(1 + x || 1);
  } 

}, 5000);

// namedFn() is undefined out here **(except for IE <= 8)**

Comments

0

Basically you're looking for something called the Y-Combinator (or as Wikipedia puts it the Fixed Point Combinator).

This blog post seems to give a good introduction (only skimmed it, not sure that I can explain it all...)

http://blog.jcoglan.com/2008/01/10/deriving-the-y-combinator/

var Y = function(f) {
  return (function(g) {
    return g(g);
  })(function(h) {
    return function() {
      return f(h(h)).apply(null, arguments);
    };
  });
};
var factorial = Y(function(recurse) {
  return function(x) {
    return x == 0 ? 1 : x * recurse(x-1);
  };
});

factorial(5)  // -> 120

Edit: I stole that from the article, and I have to admit, I find that really confusing, Y might read better as

var Y = function(f) {
    var c1 = function(g) {
        return g(g);
    };
    var c2 = function(h) {
        return function() {
            return f(h(h)).apply(null, arguments);
        };
    }
    return c1(c2);
};

And from looking at it, I'm not sure it's as simple as it should be. The biggest drawback to defining a fixpoint combinator in javascript is you need some sort of lazy evaluation so that your function will not infinitely recurse. I'll have to think about it and/or reread the article before I can post a simplified version. Of course, I'm not sure how much something like this would help you, especially performance wise. The easiest to understand (and perhaps more performant) solution is probably to create the anonymous block like others have suggested, define the function normally and return it from the block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.