0

If there is a recursive call to be made in Javascript, there are basically 2 ways to do it. First way being -

function a() {
a();
}

and second way -

function a() {
arguments.callee();
}

Questions - 1) Its given in many places that 2nd way is better than 1st, but there is no explanation.

2) Arguments.callee being deprecated, what is the alternative?

3) Is there a way to call a self invoked function recursively, and that too with the function being anonymous. like given below, without using arguments.callee or any other function inside it.

console.log((function() {
//Recursive call...how?
})()
);

1 Answers1

1

1) Unless there's a good technical reason for this (you said you read this in many places but didn't give any references) I find the first way much better as it's more clear.

2) The alternative is exactly what you called the "first way".

3) You already solved the problem giving a name to the function so it's not anonymous anymore:

console.log((function a() {
    a();
})());
Thiago Duarte
  • 941
  • 9
  • 21
  • Thanks for the answer, question 3 has been edited. pls see it. – kushal bhandari Feb 27 '15 at 20:20
  • @kushalbhandari Why don't you just give the function a name? I really don't understand the arbitrary requirement you're adding. It seems like an unnecessary complication. – tnw Feb 27 '15 at 20:26
  • @tnw - Its a question that i got from a puzzle :) – kushal bhandari Feb 27 '15 at 20:27
  • @kushalbhandari http://stackoverflow.com/questions/3883780/javascript-recursive-anonymous-function – tnw Feb 27 '15 at 20:30
  • @tnw - Other than adding a function inside it, or calling it using arguments.callee, did not find any other way. – kushal bhandari Feb 27 '15 at 20:30
  • @kushalbhandari Yeah, I haven't found anything else either. The answer might just be that there is no way to do specifically what you're asking. – tnw Feb 27 '15 at 20:31