Reputation: 3
I am studying JavaScript and I can't understand this.
function Out1()
{
function In1()
{
console.log("text inside function In1");
}
return In1();
}
function Out2()
{
return function In2()
{
console.log("text inside function In2");
};
}
Out1(); // text inside function In1
Out2(); //
Out2(); outputs nothing in console. What I am doing wrong?
Upvotes: 0
Views: 453
Reputation: 1074355
Out2(); outputs nothing in console. What I am doing wrong?
Out2
returns a reference to the function it created. It doesn't call that function. You could call it by using ()
on the returned reference:
// vv-------- These call `Out2`
Out2()();
// ^^------ These call the function referenced returned by `Out2`
E.g.:
var f = Out2(); // `f` is now a reference to the `In2` function
f(); // This calls `In2`
Upvotes: 1