Return the named function from the constructor function

I had never seen this syntax before, but used it in all of Angular's source code.

function something() {
    // do stuff...
    return function foo() {
        // do stuff..
    };
}

// later
var x = something();

From what I can say, it is foo()used as a closure function, but why does the function have a name? I thought the close function has invalid syntax, but it works fine in the browser.

Is there anything else between the code above and below? if so, then what?

function something() {
    // do stuff...
    return function() {
        // do stuff..
    };
}

// later
var x = something();
+4
source share
2 answers

This has nothing to do with the function included in the closure.

There is a real difference between function declaration :

function myFunc() { /* ... */ }

and function expression :

var myFunc = function() { /* ... */ };

or

var myObj = {
    myFunc: function() { /* ... */ },
    // ...
}

. , , :

var myFunc = function privateName() { /* ... */ };

( , privateName myFunc.)

. , :

  • IE, IE8, , .

:

  • , .

  • , , , argument.callee.

Kangax , , : , .

+5

- , ( ).

, , :

var anonNamedFn = function fn() {
    fn.cache = fn.cache || {
        callCount: 0
    };

    fn.cache.callCount += 1;
};

:

var anonNamedFn = function fn(n) {
    if (n > 0) {
        return fn(n-1);
    }
};

this.

UPDATE

, ,

, (IE9 + IE).

name = > named function Paul S.

+3
source

Source: https://habr.com/ru/post/1535749/


All Articles