0

Just trying to understand closure vs non-clousure functions. So when I run the following...

function outer()
{
     var i = 0;
     i++;
     
     console.log( i );

}

outer(); // prints 1
outer(); // prints 1
outer(); // prints 1

But when I run the following ...

function outer()
{
         var i = 0;

         function inner()
         {
                  i++;
                  console.log( i );
             }

         return inner;
}

var closure = outer();
closure(); // prints 1
closure(); // prints 2
closure(); // prints 3

That's fine. I understand the above. However, when I try to run the following ...

function outer()
{
     var i = 0;
     i++;
     
     console.log( i );

}

var closure = outer();
closure(); // prints 1
closure(); // prints 2
closure(); // prints 3

I get an error because unlike in the closure, the function is not being returned here. So, I modified it to the following ...

function outer()
{
     var i = 0;
     i++;
     
     console.log( i );

    return this;
}

var closure = outer();
closure(); // prints 1
closure(); // prints 2
closure(); // prints 3

I still get an error, only to find out that this in this context does not refer to the parent function... Rather it refers to the global object. No wonder it gave an error. But what can I replace this with, to refer to the outer() function?

Secondly, even then... Will I be able to simulate the closure? I reckon not... But why?

Grateful
  • 7,657
  • 7
  • 34
  • 59
  • 1
    The `this` keyword rarely ever refers to a function. – Bergi Nov 11 '19 at 07:50
  • I'm not sure what you are looking for, but maybe `return function inner() { console.log(i); };` or `return outer;`? – Bergi Nov 11 '19 at 07:51
  • 1
    Well, are you returning *a function* from your function? Only in one case. In the others `closure` is not a function and hence can’t be called. – deceze Nov 11 '19 at 07:54
  • 1
    In a nutshell, `this` refers to the object that the function is a part of. Not to the function itself. – deceze Nov 11 '19 at 07:57
  • 2
    *”Secondly, even then... Will I be able to simulate the closure?”* - No, because at best you’d be replicating your first case. You can’t simulate a closure without writing a closure. – deceze Nov 11 '19 at 07:58
  • @deceze Thank you for that.... And the rest of you too. Much apprecaited. – Grateful Nov 11 '19 at 08:47
  • This post could clarify some of your doubts https://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1 – Alan Lal Nov 12 '19 at 05:20

1 Answers1

0

This code var closure = outer() will execute the outer function and will assign the result to closure. But since there is nothing returned from outer closure() will not work as it expects a function. In this case you can try by removing () from outer.

Also in this case returning this is same as returning nothing. Inside this function this revers to window object so closure() is becoming this()

function outer() {
  var i = 0;
  i++;
  console.log(i);
  return this;
}

var closure = outer;
console.log(closure());
closure();
closure();
I get an error because unlike in the closure,
the function is not being returned here. So,
I modified it to the following ...
brk
  • 43,022
  • 4
  • 37
  • 61