0

Lets say, I have defined a function like

var f = function(cb){cb()};

Now, if I pass a callback function it will work:

f(()=>{console.log("ccb")}); //print: ccb

But if I pass a argument, in this case x will be undefined:

f((x)=>{console.log("x:"+x);}); // x will be undefined

so one solution is to use closure,

function cba(x){
      return function(){
        console.log("ccbbaa:"+x)
      }
}

f(cba(20)); //will work give output: ccbbaa:20

But if I am trying to achieve closure using inplace function, considering xx is defined.

var xx = 20;
f(function(xx){
    return function(){
        console.log("xxx: "+xx)
    }
});

callback inside f is not even called. Why? How will we can use this inline function to make it work? I am studying closures so wanted to understand this. Any help is appreciated.

4
  • You execute the outer function with signature function(xx) which returns a new function with signature function() but you never execute the latter. Since it's also never returned, you can't invoke it afterwards, either. It's just produced and at some point garbage collected. Commented Jan 27, 2020 at 11:47
  • "callback inside f is not even called. Why?" — Because you've done nothing that would call it. f() doesn't do anything with the return value it gets from calling the function passed to it. Commented Jan 27, 2020 at 11:48
  • @Quentin How we can change it to make it work? Commented Jan 27, 2020 at 11:53
  • 1
    @NikhilKumar — You've laid down a very abstract and broken solution to a problem that isn't really clearly defined. It's hard to say how to solve the actual problem because we don't know what it is. You just seem to be wanting f to do something f isn't designed to do at all and are approaching it by changing what you pass to f … but no matter what you pass to f it isn't going to change how f works. Commented Jan 27, 2020 at 11:56

4 Answers 4

2

In your code you're not calling your inline function with xx. You're just passing the inline function to f which will be executed it but that will not print anything because the called inline function simply returns another function which is then never called.

var f = function(cb){cb()};
var xx = 20;
f((function(xx){
    return function(){
        console.log("xxx: "+xx)
    }
})(xx));

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

4 Comments

Thankyou for swift response and sorry for naive question, But as per that logic, why f(cba(20)); worked
@NikhilKumar because you execute cba which then returns a (single-level) function. The exact same thing happens if you have (function(xx){ return function(){ console.log("xxx: "+xx) } })(xx) - you have a function that is executed and returns a (single-level) function. The only difference is that in the latter case you have an IIFE but nothing is really fundamentally different - you get the same result either way.
@VLAZ Oh got it now, Thankyou. Wondering how can we use arrow function in that case, like ` f((xx)=>{return function(){console.log("xxx: "+xx)}}(xx));`. Is it correct?
@NikhilKumar Yes, that would work syntactically. But the question is why you want an IIFE at all? it provides no benefit over using a normal function. If needed, there is partial application which can be used to pre-set parameters that the function expects. With this (and other techniques, like currying), you can take a normal function that takes parameters and produce thunks that are essentially functions that don't expect a parameter but you can execute later.
0

Use bind to bind the parameter:

const f = function(x){x()};
const param = 12

f(function(x){
    console.log("ccbbaa:"+x)
}.bind(null, param))

Comments

0

The only way to make this work is to give the anonymous function a default value for xx

f(function(xx=20){
    (function(){
        console.log("xxx: "+xx)
    })(xx);
});

2 Comments

This is still not going to execute the inner function that is returned from the outer.
It doesn't work because f will always return undefined because it doesn't return anything. The inner function is never returned after function(xx) is executed by f. To make it work, you need to execute the inner function. I've edited the code to make it work.
0

inline function returns another function which is never called.

 var f = function(cb){cb()};
    var xx = 20;
    f((function(xx){
        return function(){
            console.log("xxx: "+xx)
        }
    })(xx));

Comments

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.