0
function foo(b)
{
    return cool
    (
        function(x)
        {
            if(x)
            {
                b(x);
            }
        }
    );
}

where cool is a function that takes in a function. This code works well enough. How can I make this work though?

function bar(x)
{
    if(x)
    {
        b(x);
    }
}
function foo(b)
{
    return cool(bar);
}

I want to do this because bar is a frequently used function from functions that are like foo. Is there any way to open up the scope further so that bar can see b from foo?

2
  • Unfortunately I don't have access to the cool function. Oh well it was worth asking. Commented Jan 14, 2015 at 2:58
  • Ah, I misread the code, please disregard that. I thought you were returning a function called cool. Take a look at Daniel's answer below. Commented Jan 14, 2015 at 2:59

1 Answer 1

3

Wrap bar in a function that takes b as an argument: i.e.

function baz(b)
{
    return function(x){
        if(x)
        {
            b(x);
        }
    }
}

function foo(b)
{
    return cool(baz(b));
}
Sign up to request clarification or add additional context in comments.

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.