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?

Community
  • 1
  • 1
Shibasis Sengupta
  • 629
  • 1
  • 6
  • 21
  • 3
    `multFunc1` returns a function, so you would have to do `multFunc1()(6)` in order to get `12`. – 4castle Feb 19 '17 at 04: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). – nnnnnn Feb 19 '17 at 04:18
  • @4castle ..The call multFunc1()(6) actually returns undefined. I actually had tried that already – Shibasis Sengupta Feb 19 '17 at 09:46

2 Answers2

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

Ant's
  • 13,545
  • 27
  • 98
  • 148
  • 1
    Note that the inner function shown always returns `undefined`. – nnnnnn Feb 19 '17 at 04:20
  • @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. – Shibasis Sengupta Feb 19 '17 at 09:53
  • @ShibasisSengupta: Please check my answer fully. You have missed a `return` statement, thats the reason your getting `undefined` – Ant's Feb 19 '17 at 12:13
  • @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? – Shibasis Sengupta Feb 20 '17 at 12:03
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);
Chris Cruz
  • 1,829
  • 13
  • 15
  • 1
    `maltFunct1()(6)` returns `undefined`. (The inner function always returns `undefined`.) – nnnnnn Feb 19 '17 at 04:20
  • My fault, thank you for pointing that out. I completely overlooked that.. – Chris Cruz Feb 19 '17 at 04:22
  • 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 Feb 19 '17 at 09:57
  • @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 Cruz Feb 19 '17 at 18:28
  • @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). – Shibasis Sengupta Feb 20 '17 at 11:59
  • @Shibasis Sengupta of course, I'm happy to help. Also, I completely understand the low score voting situation as I was there just a couple weeks ago. If you consider this to be an answer, you could of course select this as the answer though. If not, I'd like to further assist you so it can become the answer. – Chris Cruz Feb 20 '17 at 16:18
  • @Chris thanks..yes I think this should be the answer – Shibasis Sengupta Feb 22 '17 at 05:14
  • @ShibasisSengupta great, please accept my response as the answer. I'm glad I could help you figure it out. – Chris Cruz Feb 22 '17 at 05:17