+ 2

Please why isn't the function call correct in the closure example below

https://code.sololearn.com/We4ILj6oO9Ak/?ref=app

24th Mar 2018, 8:13 AM
Sammy
Sammy - avatar
5 Antworten
+ 1
Hi Sammy Not sure what you are trying to do, but this may help. Your first return is causing some problems as it will exit the inner function before the give function will execute var addTo= function(passed){ var add = function(inner){ //return passed + inner; var give=function(give){ let tmp=give+2; console.log("give func"+tmp); return tmp; }; console.log("inner func"); return give; //why isn't this returning 8 }; return add; }; console.log(((addTo(1))(2))(3)); //nested IIFEs
24th Mar 2018, 10:41 AM
Mike Choy
Mike Choy - avatar
24th Mar 2018, 8:59 AM
josh mizzi
josh mizzi - avatar
+ 1
After a bit more reading, I think you are after this interview question. // Lexically nested function definitions defined within enclosing function function Sum(arg0) {   function Inner1(arg1) {     function Inner2(arg2) {       return arg0 + arg1 + arg2;     }     return Inner2;   }   return Inner1; } console.log(Sum(3)(4)(5));
25th Mar 2018, 6:24 PM
Mike Choy
Mike Choy - avatar
0
thanks everyone... you've all helped
26th Mar 2018, 1:14 AM
Sammy
Sammy - avatar