0

I am confused with the scoping of closure expression. I don't understand why output is 40 only.

js

 function multiply(input) {
 var no = 5;
 function multiply2(mul) {
 mul *= input;
 return no * mul;
 }
 return multiply2;
 }
 
 var total = multiply(4);
 var result = total(2);
console.log("result :",result);

Output

result :40

Brian Tompsett - 汤莱恩
5,92772 gold badges63 silver badges135 bronze badges
asked Jun 15, 2016 at 1:48
5
  • 1
    What is the expected value? What exactly is the problem? What exactly don’t you understand? Commented Jun 15, 2016 at 1:53
  • I could't find out how the output is 40. Commented Jun 15, 2016 at 1:55
  • 1
    But you wrote it at the beginning of the question: 2 * 4 = 8 and 8 * 5 = 40. Commented Jun 15, 2016 at 1:58
  • I knew the answer so I assumed that like something like formula is happening.But I don't know why? Commented Jun 15, 2016 at 2:01
  • Because mul *= input; and return no * mul;. Now you need to look at what the values of mul, input and no are. Commented Jun 15, 2016 at 2:06

3 Answers 3

1

multiply(4) returns a function multiply2 which has two free variables in scope: no which has the value 5, and input which has the value 4. This line assigns that returned function to total:

var total = multiply(4);

total(2) calls that function, passing in 2 for the argument mul. The definition of that function is two statements, but it could be simplified to one statement (since mul is a local variable and is never used after modifying it, that side-effect can be safely dropped): return no*mul*input;. mul was passed in, with the value 2; no and input are the free variables that were created when multiply(4) was called and we know what they are set to, so, in the statement below, result becomes 5*2*4, which is 40.

var result = total(2);
answered Jun 15, 2016 at 1:57
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explaination.It helps me to understand the concept.
0

The output 40 is correct. Your functions multiply 4*2 = 8 and then you multiply 8*5 = 40. I think if you aren't getting what you expect is because you are multiplying 4*2 and making mul = 8 so what then gets returned is 5*8

answered Jun 15, 2016 at 1:59

Comments

0

This is functional programming called currying. An abstract implementation would be y = f(m) => g(n) => m*n. So your input to f will be carried into g and return a function which is g(n). Then your input to g will return the final result.

This can be implemented more than one level. ie. y = f(m)=>g(n)=>h(p)=>m*n*p, where the input value will be carried to next function until the final input.

Here is some basics of functional programming. Also a great book here.

answered Jun 15, 2016 at 2:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.