getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

a closure associates a single function with a set of state #186

Closed a-c-sreedhar-reddy closed 5 years ago

a-c-sreedhar-reddy commented 5 years ago

This statemet is not correct I guess.

a closure associates a single function with a set of state, whereas an object holding the same state can have any number of functions to operate on that state

function closur(){
  let i = 0;
  return {
   add : ()=>i++,
   sub : ()=>i--
 }

Here two functions are assosiated with a state. Correct me if my understanding is wrong.

getify commented 5 years ago

Yes, there are two functions, and thus two closures, each closed over the same shared state.

a-c-sreedhar-reddy commented 5 years ago

Then is this statement

a closure associates a single function with a set of state, whereas an object holding the same state can have any number of functions to operate on that state

which I found in this book wrong?

getify commented 5 years ago

I think you're reading too much into the statement. The intent is not to imply that a function closure is mutually exclusive of another function closure. It's to point out that closure is over private state, so only the function(s) closed over it can access it, where as an object is public state and any function at any time can access it.

a-c-sreedhar-reddy commented 5 years ago

Thank for the book.:heart:

I also heard FP helps scalability. If possible can you just brief about that in the book.