4 ms·
It's funny how closures are in-fashion and global mutable state is out-of-fashion, when the latter is really a special case of the former.
by xamuel 9y ago
It's funny how closures are in-fashion and global mutable state is out-of-fashion, when the latter is really a special case of the former.
- deleted 9y ago[deleted]
- SCdF 9y agoHow are they related sorry? I know in ES6 closures inherit the scope they are declared in, but I don't think that's a required part of the concept is it?
- xamuel 9y agoConsider this ES6 program: (function(){let x=0; let f=()=>{x=1}; f(); console.log(x);})() Is x a "global mutable state" variable? If the above is the entirety of the program, then essentially yes. If you say "no, x is not global mutable state", then "global mutable state" is pretty meaningless in JS, since any script can be wrapped like above.
- fleitz 9y agoMost closures don't reference global mutable state. Generally they take immutable copies and you have to tell the compiler that you want mutable state which is generally limited to the scope of the function the closure appears in.
- olavk 9y agoThat's like saying global variables are just a special case of variables, so it is "funny" global variables are discouraged while local variables and parameters are accepted.