Giter VIP home page Giter VIP logo

Comments (5)

jrhorn424 avatar jrhorn424 commented on July 26, 2024

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
 }
}

from js-functions-ins-and-outs.

gaand avatar gaand commented on July 26, 2024

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
  }
} 

from js-functions-ins-and-outs.

gaand avatar gaand commented on July 26, 2024
const bob = memoFactory('bob')
bob()
// => 'bob'
const dave = memoFactory('dave')
dave()
// => 'dave'
bob === dave
// => false
bob()
// => 'bob'
dave()
// => 'dave'

from js-functions-ins-and-outs.

MicFin avatar MicFin commented on July 26, 2024

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.

from js-functions-ins-and-outs.

gaand avatar gaand commented on July 26, 2024

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.

from js-functions-ins-and-outs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.