javascript 不是嵌套函数范围扩大
javascript not nested function scope widening
function foo(b)
{
return cool
(
function(x)
{
if(x)
{
b(x);
}
}
);
}
其中cool
是一个接受函数的函数。这段代码运行良好。我怎样才能使它工作?
function bar(x)
{
if(x)
{
b(x);
}
}
function foo(b)
{
return cool(bar);
}
我想这样做是因为 bar
是类似 foo
的函数中经常使用的函数。有什么方法可以进一步打开范围,以便 bar
可以从 foo
看到 b
?
将 bar
包装在一个以 b
作为参数的函数中:即
function baz(b)
{
return function(x){
if(x)
{
b(x);
}
}
}
function foo(b)
{
return cool(baz(b));
}
function foo(b)
{
return cool
(
function(x)
{
if(x)
{
b(x);
}
}
);
}
其中cool
是一个接受函数的函数。这段代码运行良好。我怎样才能使它工作?
function bar(x)
{
if(x)
{
b(x);
}
}
function foo(b)
{
return cool(bar);
}
我想这样做是因为 bar
是类似 foo
的函数中经常使用的函数。有什么方法可以进一步打开范围,以便 bar
可以从 foo
看到 b
?
将 bar
包装在一个以 b
作为参数的函数中:即
function baz(b)
{
return function(x){
if(x)
{
b(x);
}
}
}
function foo(b)
{
return cool(baz(b));
}