不知道如何正确解释这一点,但基本上我有:
mg.postGiApi("Dashboard", "postChartOsTypeData", { groupIdList: that.idList }, function (data) {
that.tempName(data, "osType", cb);
});
我希望它看起来像:
mg.postGiApi("Dashboard", "postChartOsTypeData", { groupIdList: that.idList }, that.tempName.someExtendingFunction("osType", cb));
我正在寻找可以让我执行此操作的“someExtendingFunction”。这有可能吗?没什么大不了的,但可以清理事情。
谢谢
最佳答案
没有 native 函数可以执行此操作,但您可以编写类似的函数:
function someExtendingFunction(context, name, type, cb) {
return function(data) {
context[name].call(context, data, type, cb);
};
}
mg.postGiApi("Dashboard",
"postChartOsTypeData",
{ groupIdList: that.idList },
someExtendingFunction(that, "tempName", "osType", cb));
请注意,that.tempName.someExtendingFunction(…)
永远不会工作,因为 that
上下文将会丢失。如果您将 someExtendingFunction
作为 (Function.prototype
) 方法调用,则需要显式提供上下文,例如 bind
做到了。
关于javascript - 向作为参数的函数添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25753666/