1

I just started learning js from EloquentJavascript I need 2 answers to help me pass this example:

function makeAddFunction(amount) {
  function add(number) {
    return number + amount;
  }
  return add;
}

var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
show(addTwo(1) + addFive(1));

Question 1:

Can we just add () after variable meaning to add argument to the function that the variable represent? like addTwo(1) means makeAddFunction(1)?

Question 2:

makeAddFunction(2) means amount = 2, then what's the value for number? If I pick a section of the above example, what will return in the following section?

function makeAddFunction(2) {
  function add(number) {
    return number + 2;
  }
  return add;
}
3
  • Did you try the code? see what it does. Commented Jun 9, 2012 at 22:23
  • the first example returns 9, the 2nd example returns ReferenceError: amount is not defined. Commented Jun 9, 2012 at 22:25
  • You're doing something wrong. show us a fiddle. And the question is too localized, You're asking a question about a concrete code... Commented Jun 9, 2012 at 22:27

2 Answers 2

2

Can we just add () after variable meaning to add argument to the function that the variable represent? like addTwo(1) means makeAddFunction(1)

Nope. addTwo(1) equals to makeAddFunction(2)(1)

makeAddFunction(2) means amount = 2, then what's the value for number? in the following example2, what will return?

You pass it additionally, when call addTwo or addFive

Sign up to request clarification or add additional context in comments.

5 Comments

So, when there's additional var inside a function, we can pass additional value like makeAddFunction(2)(1) ?
@Jenny: it's not additional var. makeAddFunction returns a function, that accepts one argument. So result of makeAddFunction(1) call is another function, that accepts one argument
I see. so, in this case, I need to give argument to the another function by adding the argument to the var addTwo and addFive?
@Jenny: yes, because they are functions
Thanks! This helps get my head straight.
2

Both of your questions are answered if you look at the return type of makeAddFunction(amount). You are returning a function add(number). So by defining addTwo as makeAddFunction(2), you are returning the following function:

function add(number) {
    return number+2;
}

If addTwo and addFive are function, show(...) will show: 9. (2+1)+(5+1)=9

2 Comments

addTwo and addFive are not function. they are variables defined just below the function makeAddFunction. That's why I doubt if I can pass argument through variable by adding () after variable.
@Jenny: they are functions. You can store function in a variable. var foo = function() { return '123'; }; alert(foo()); --- this will alert 123

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.