简体   繁体   English

javascript未嵌套功能范围扩大

[英]javascript not nested function scope widening

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

where cool is a function that takes in a function. cool是一个接受功能的函数。 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 . 我想这样做是因为bar是像foo这样的函数中经常使用的函数。 Is there any way to open up the scope further so that bar can see b from foo ? 有什么办法可以进一步打开作用域,以便bar可以从foo看到b

Wrap bar in a function that takes b as an argument: ie 在以b为参数的函数中包装bar :即

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

function foo(b)
{
    return cool(baz(b));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM