我试图将一个带有两个参数的函数作为参数传递给另一个函数(一种回调)。我有一个不可更改的功能(" F"),另一个是前一个的包装器,我可以用我喜欢的方式编码(" G") 。我试图尽可能地简化代码,只留下它的重要部分:
"use strict"
// this function can't be changed in any way
var F = function (a, b)
{
return new G(function (success, error)
{
var sum = a + b;
if (sum > 0)
{
// EXCEPTION: point of failure, since we can't reach G.success
success(sum);
}
else
{
// of course we'd fail here too
error(sum);
}
});
};
var G = function (f)
{
this.success = function (result)
{
console.log("The sum is positive: " + result);
}
this.error = function (result)
{
console.log("The sum is negative: " + result);
}
return f();
};
var result = F(10, 5);