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?
})()
);
3
  • 2
    Where did you read that the 2nd way is better? As for Question 2, you already listed an alternative. Commented Feb 27, 2015 at 20:08
  • 1
    possible duplicate of javascript: recursive anonymous function? Commented Feb 27, 2015 at 20:10
  • I did not get the answer from both above comments Commented Feb 27, 2015 at 20:17

1 Answer 1

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();
})());
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, question 3 has been edited. pls see it.
@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 - Its a question that i got from a puzzle :)
@tnw - Other than adding a function inside it, or calling it using arguments.callee, did not find any other way.
|

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.