0

As suggested by many people, one of the usages of named function expression is for recursively calling itself. However, it seems that in Chrome Console, function expression without a name can still do so.

Edit : I know this is gonna be stackoverflow, however, I would expect a output like a() is not a function instead of Uncaught RangeError: Maximum call stack size exceeded(…).

var a = function () { 
        a();
        }
a();

the following function expression with a name should be giving me a Uncaught RangeError: Maximum call stack size exceeded(…).

var a = function a () { 
           a();
        }
a();

Edit 2 : In this link https://developer.mozilla.org/en/docs/web/JavaScript/Reference/Operators/function, it says that "If you want to refer to the current function inside the function body, you need to create a named function expression.". However, it seems to me that the statement is no true, because you can still refer to the current function inside the function body without assigning a function identifier to it

Any thoughts would be appreciated

Xuzheng Wang
  • 531
  • 5
  • 17
  • And your question is....? – Ionut Necula Oct 05 '15 at 13:46
  • Thanks for your comment, maybe I did not make my question clear. My question is since the `function expression without name` can call itself recursively, why do we need a `named function expresion` – Xuzheng Wang Oct 05 '15 at 13:47
  • Possible duplicate of [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Ionut Necula Oct 05 '15 at 13:53
  • 1
    Consider if the expression is not assigned to a variable; `someCallback(function a() { a(); })` vs `someCallback(function () { ??? })` – Alex K. Oct 05 '15 at 13:57
  • so, when you call `a()` - `a` is already function :-) so all correct – Grundy Oct 05 '15 at 14:02

2 Answers2

3

You are reaching the stack limit because there are no conditions to limit the recursion.

var a = function (i) { 
        console.log(i);
        if (i >= 10) {
          return;
        }
        else {
          a(++i);
        }
}
a(0);

The above is a better example to show an actual working example of this recursion. Notice how there is a check here whether or not to call the recursive function. The output would be the following:

0
1
2
3
4
5
6
7
8
9
10

You can also have this logic successfully defined at parse time:

function a (i) { 
        console.log(i);
        if (i >= 10) {
          return;
        }
        else {
          a(++i);
        }
}
a(0);

As for the scope of the function definition, this example shows when a() would be defined:

if (typeof a === 'undefined') {
  console.log('a is undefined before the definition');
}
else {
  console.log('a is defined before the definition');
}

var a = function () {
  if (typeof a === 'undefined') {
    console.log('a is undefined inside of a');
  }
  else {
    console.log('a is defined inside of a');
  }
}

a();

if (typeof a === 'undefined') {
  console.log('a is undefined after the definition');
}
else {
  console.log('a is defined after the definition');
}

The output to this snippet is the following:

a is undefined before the definition 
a is defined inside of a 
a is defined after the definition
Thomas Stringer
  • 5,682
  • 3
  • 24
  • 40
  • Thanks for your response. What I meant was that `var a = function () { a(); }` is a function expression without name, which is a anonymous function, so it should not be able to refer to itself inside its function. I expected something like a() is not a function in the output – Xuzheng Wang Oct 05 '15 at 13:52
  • @XuzhengWang See my edit, that should explain why that is possible. – Thomas Stringer Oct 05 '15 at 13:57
  • Thank you very much, my question lies in the second output, why a is defined inside of a . In this link https://developer.mozilla.org/en/docs/web/JavaScript/Reference/Operators/function, it says that "If you want to refer to the current function inside the function body, you need to create a named function expression.". However, it seems to me that the statement is no true, because you can still refer to the current function inside the function body without assigning a function identifier to it. – Xuzheng Wang Oct 05 '15 at 14:05
  • @XuzhengWang, really you refers not to self function, but on variable `a`, so if you assign to it some another value, it break work :-) – Grundy Oct 05 '15 at 14:10
0

This is a really old thread but I will post something similar, since there is a statement on the book Javascript: Object Oriented Programming by Kumar Chetan Sharma, Stoyan Stefanov, and Ved Antani That says this: "Function declaration cannot have recursive calls" That statement is wrong or lacks of a more comprehensive explanation because this still does work.

var facto = function(n){
    if(n<=1) return 1;
    return n * facto(n - 1);
}

console.log(facto(3)) // 6