Help creating complex objects/collections using closure

Hello there,
I am studying JS and I have def hit my first roadblock. This is/are closures.
I understand what a closure is. I guess I am just having some issues wrapping my head around syntax(which has always been the issue for me).

function solve(){
  let a = 0;
  return function(){
    a++;
    console.log(a);
  }
}

let incrementor = solve();
incrementor();
incrementor();

This is just a simple program I have created to increment a value. I understand everything that is happening here. No mysteries thus far, but I was also looking at some challenges online and how to expand the logic.

Let’s say that firstly we want to accept some input. Where do I add input as a parameter? To the first function solve? To the second function? Or to both? I want to create a program which can do this:

And this is just the start. Let’s say I want to create a collection. I know how to return an object:

function solution(){
  let state = '';

  return {
    append(str){
      state += str;
    },
    print(){
      console.log(state);
    }
  }
}

let value = solution();
value.append('hey there');
value.print();

This works completely fine.

But when I try to do this:

I am stumped again. I know everything in JS is pretty much an object even the arrays. That does not appear to help me. If someone can lend some assistance that would be great. I don’t want someone to jus write the code for me - just some explanations or pointers.

What is this line doing?

let incrementor = solve();

What is the value of incrementor after solve is executed? Well, solve returns a function, so incrementor is now just another name for that returned function. That’s why you can put parens after it to call it as a function.

If you want to pass input into a function, how do you do that? You pass it in using a parameter list:

myFunction(param1, param2, ...)

So to pass input into the function that is now called incrementor you would do:

incrementor(param1)

Usually, if a function accepts parameters then you add them to the function definition. For example, if you were going to create a function that takes two arguments, you would define it as:

function myTwoArgFunction(arg1, arg2) {
  ...
}

So if you wanted the function that is returned by solve to also accept inputs then how would you change it to do so?

Hello!
Thank you for taking the time to answer me.

function solve(){
  let a = 0;
  return function(input){
    a+=input;
    console.log(a);
  }
}

let incrementor = solve(18);
incrementor(1);
incrementor(2);

This outputs 1 and 3 respectively. But the first argument(18 or whatever I input in) still does not influence the function. Idk I am still hugely confused, I have no idea how many videos and articles I read about this.

function solve(input){
  let a = 0;
  return function(){
    a+=input;
    console.log(a);
  }
}

let incrementor = solve(18);
incrementor(1);
incrementor(2);

This outputs 18 and 36 respectively. I think I am more confused now actually:D

In your first example, look at your definition of solve, you haven’t added a parameter in the parens so you aren’t able to pass in a value. You are merely setting a to 0 and so that’s where a will always start. If you want to set a to something else then you need to add a parameter to the function so you can pass that starting value into the function.

Both the solve function and the function it returns can have function parameters.

In your second example, you don’t have any function parameters defined for the function being returned, so when you call:

incrementor(1)

The 1 you are passing into incrementor isn’t being used. But when you initially called solve(18) you set input to the value 18 and thus when the inner function is executed it is using 18 for the value of input. So the first time you call incrementor it adds 18 to a (which is 0 the first time) and thus it logs 18. The second time you call incrementor it adds another 18 to a and thus it logs 36.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.