iPzard
iPzard

Reputation: 2368

Is there a way to reference a function declared inside a variable?

This question is more out of curiosity. Is there a way I can call bar() without calling foo() in the code below? Or does this completely anonymize bar()? Furthermore, is there any use case for this sort of syntax?

var foo = function bar() {
    console.log('test')
}

foo(); // 'test'

Upvotes: 2

Views: 58

Answers (1)

user9166941
user9166941

Reputation:

It's because it's scoped inside the function. You can test like this.

var a = 1;
var foo = function bar() {
    a++;
    console.log('test');
    if(a < 3) {
        return bar();
    }
}

foo();
//test
//test

Upvotes: 2

Related Questions