The diagram in your article is very good. Function + surrounding state
A closure is a function that remembers the variables from the place where it was defined is a simpler way of saying that IMO which can be understood by everyone
If that definition were correct, there would be no point having separate words for closure and function, since every function has access to its surrounding scope in this way.
No, they donβt. They access the variables again which are in their lexical scope. In Closure, they store the variables and their values. Which is what your diagram signifies - function + state
To check this - just define a global variable console log in a function change the value and try again. New value will be printed meaning it doesnβt remember it accesses.
Every function forms a closure with the lexical scope in which it was created. If a variable changes in that lexical scope, it will see the changed value. Make a JS module, export a function that accesses a module level var - this, again, works due to closures. Change the module level var, the change will be reflected. Often when code takes advantage of closures, it does so within the lexical scope of another function, which it still has access to when that function finishes execution. Closures work the same everywhere.
Your example with the global scope just reinforces the point. The global scope is still active and accessible, so vars there can be easily changed.
Variables are not 'stored' in a closure, a closure has references to the function's lexical scope.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The diagram in your article is very good. Function + surrounding state
A closure is a function that remembers the variables from the place where it was defined is a simpler way of saying that IMO which can be understood by everyone
Not really...
If that definition were correct, there would be no point having separate words for closure and function, since every function has access to its surrounding scope in this way.
Remembers is the keyword here
Every function has access but not every function remembers
They do. All functions are the same in this respect.
From MDN:
Every function has an associated closure, remembering the lexical environment in which it was created.
No, they donβt. They access the variables again which are in their lexical scope. In Closure, they store the variables and their values. Which is what your diagram signifies - function + state
To check this - just define a global variable console log in a function change the value and try again. New value will be printed meaning it doesnβt remember it accesses.
Every function forms a closure with the lexical scope in which it was created. If a variable changes in that lexical scope, it will see the changed value. Make a JS module, export a function that accesses a module level var - this, again, works due to closures. Change the module level var, the change will be reflected. Often when code takes advantage of closures, it does so within the lexical scope of another function, which it still has access to when that function finishes execution. Closures work the same everywhere.
Your example with the global scope just reinforces the point. The global scope is still active and accessible, so vars there can be easily changed.
Variables are not 'stored' in a closure, a closure has references to the function's lexical scope.