0

I have been recently reading function expression and declaration in javascript and have referred to quite a few online articles about this. I also have seen quite a few discussion about this topic on SO. In the process of learning I tasked myself with a challenge, which I am not able to clearly explain. Can I kindly request SO experts to help me gain some insight here?

Here is the problem scenario -

Scenario 1:

    >var multFunc=function(n){var a=2; return n*a;}
    >multFunc(6)    
     12

I understand this scenario and the result is what I was expecting (12).

Scenario 2:

>var multFunc1=function(n){return function(n){n*2}}
>multFunc1(6)
function (n){n*2}

I did not understand the second case. Why would it not return 12? Can someone please help me understand this? I have checked this link - Javascript Function Expressions, this link JavaScript Nested function and I also did ask a similar question yesterday, but I guess I did not fully grasp the concept (as explained graciously by T.J) - Trying a closure the wrong way?

3
  • 3
    multFunc1 returns a function, so you would have to do multFunc1()(6) in order to get 12. Commented Feb 19, 2017 at 4:13
  • Further to what @4castle said, note that the argument passed to multFunc1 isn't used, and the inner function shown always returns undefined (because it doesn't have a return statement). Commented Feb 19, 2017 at 4:18
  • @4castle ..The call multFunc1()(6) actually returns undefined. I actually had tried that already Commented Feb 19, 2017 at 9:46

2 Answers 2

2

The code:

var multFunc1=function(n){return function(n){n*2}}

returns a function. So multFunc1 represents the returned function, in this case:

function(n){n*2}

so you had to call like:

multFunc1(1)(2)

So basically the returned function remembers the value of n (passed argument, I recommend you to read about closures). So we can re-write the calls like:

var multFunc1=function(n){return function(x){n*x}}
var multBy2 = multFunc1(2)
var multBy16 = multFunc1(16)

multBy2(4) // 8
multBy16(2) // 32

Side note: The multFunc1's inner function, doesn't have any return statement, so it always returns undefined as @nnnnnn pointed out in comments

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

4 Comments

Note that the inner function shown always returns undefined.
@Ant's thanks for the answer but are you sure about this answer? First of all this call - multFunc1(1)(2) returns undefined. And if I do this - var multFunc1=function(n){return function(n){n*2}} and then var multBy2 = multFunc1(2); the call multBy2(4) returns undefined.
@ShibasisSengupta: Please check my answer fully. You have missed a return statement, thats the reason your getting undefined
@Ant's thanks for your answer. The real problem should be solved changing the inner function to this I guess? return function(n){ return n*2}. If that's what you meant?
1

What you are essentially doing here in the second scenario is returning the function Object. Rather than returning the result of the execution of the function (which would be usually be 12) you are just returning the Reference to that object.

UPDATE: I think you are missing the return statement inside the second function. By adding so, this yeilds the result I believe you are looking for.

var multFunc1=function(n){
    return function(n){ return n*2}
}

// The first set of () require no argument as
// they are never used withing the second function. 
multFunc1()(6);

8 Comments

maltFunct1()(6) returns undefined. (The inner function always returns undefined.)
My fault, thank you for pointing that out. I completely overlooked that..
So if in the second case, its a reference to the returned funtion object, can I not invoke the function from the reference itself with parameter?
@Shibasis Sengupta please see my update/edit to my original answer. I think this may be what you are looking for and should make more sense.
@Chris thanks for taking time to post this. I think this makes sense. I did an upvote to your answer, but SO is not displaying that owing to my low score (<15).
|

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.