Open jrhorn424 opened 7 years ago
Originally reported by @MicFin at my request over in #48:
This example may be incorrect to show error and then fix but it is not clear. https://github.com/ga-wdi-boston/js-functions-ins-and-outs#code-along---return-new-functions
- const memoFactory = function (memo) { + const memoFactory = function () { let total = 0 - return function () { + return function (memo) { total+= 1 return total + ": " + memo } }
I don't know where you example came from, it's too complicated to start with and I don't know who wrote the code, but this simple closure is the point, and is the code I wrote back whenever:
const memoFactory = function (memo) {
return function () {
return memo
}
}
const bob = memoFactory('bob')
bob()
// => 'bob'
const dave = memoFactory('dave')
dave()
// => 'dave'
bob === dave
// => false
bob()
// => 'bob'
dave()
// => 'dave'
I believe a counter was added because developers were struggling with why something like this would be used when you could just do...
const bob = 'bob'
const dave = 'dave'
bob
// => 'bob'
dave
// => 'dave'
Silly, but it is something new developers think of.
The point is to show that the name memo
in the returned functions point to different objects. You wouldn't do exactly this in the real world, but showing a different way to accomplish the same thing is often valuable. two parameters, This is the intro to closure. The counter example is where you're supposed to end.
See annotations for questions