-2

According to me: Closure is something that js creates separately to simulate the surrounding variables as if the function was being run as a normally called function so that we can pass functions as arguments etc. What I mean by normally? ..when a function is executed in situations where an external environment is not required.
I have a piece of code that uses closure:

var x=1;
var f= function()
{
    var a={result:0}
    if(x==2)
        a={result:1};
    return function g ()
    {
        console.log(a.result);
    }
}
returned=f()
x=2;
returned();
and another similar code which uses lexical scope variable instead of a closure:

var arr=[1,2,3];
var f= function()
{
    
    return function g ()
    {
        console.log(arr);
    }
}
var returned=f();
arr=[4,5,6];
returned();

In what situations a closure is used instead of the lexical scope variable?

  • I was not even aware of the different terms you used. Is it important to know them? In my opinion you should never make things too complicated or apply a certain "pattern" just because you read about it in a textbook. Always use the simplest form of code that will produce the required result - whatever it may be called. – Carsten Massmann Feb 21 '23 at 19:36
  • But there is a conceptual thing here....closure vs scope, what does js choose? – Sourabh Yadav Feb 21 '23 at 19:39
  • 1
    Both of your examples use closures, so what exactly is your question? – Đinh Carabus Feb 21 '23 at 19:40
  • 1
    I'm not sure that the two examples are even comparable. Do you find either result surprising? – General Grievance Feb 21 '23 at 19:40
  • As far as I know, closure and scope are different, in first example closure is used obviously to get a.result but my question is when would a variable would be used from scope instead of the closure? – Sourabh Yadav Feb 21 '23 at 19:44
  • 1
    Closures and scopes are entirely separate concepts. It's not clear what your two code examples are meant to demonstrate, or why you think one involves "lexical scopes" and the other does. They both involve scopes, they both involve closures. – user229044 Feb 21 '23 at 19:47
  • I really don't understand your definition of closure. Please read the first mozilla.org link in the answer below. It does a very thorough job explaining what a closure is. – General Grievance Feb 22 '23 at 06:35

1 Answers1

1

There is no situation like that. Suggest you to read a few articles on closures. You can read answers on SO too.

Your code behave differently for only one reason that is the expression x==2 is evaluated before you set x to 2. Had the value been set the value before this runs returned=f(), then a would have become {result:1}. And your expected value would have been 1.

var x=1;
var f= function()
{
    var a={result:0}
    if(x==2)
        a={result:1};
    return function g ()
    {
        console.log(a.result);
    }
}
//x=2; //Uncomment this and comment the below
returned=f()
x=2; 
returned();

For more info : 1. 2.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • As far as I know, closure and scope are different, in first example closure is used obviously to get a.result but my question is when would a variable would be used from scope instead of the closure? – Sourabh Yadav Feb 21 '23 at 19:45
  • @SourabhYadav It's the same variable, whether inside or outside the closure. – user229044 Feb 21 '23 at 19:49