javascript - 向作为参数的函数添加参数

标签 javascript

不知道如何正确解释这一点,但基本上我有:

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/

相关文章:

javascript - JQuery - 获取选择器中元素的数据属性值,而不是 attr

javascript - 奇怪的 SVG 悬停行为

javascript - jQuery UI 工具提示点击后不会消失

javascript - Codedrops 透视导航

javascript - 在reactjs中以编程方式取消选中复选框

javascript - 是否可以使用 Javascript 在音频文件中找到一段静音?

javascript - 根据关闭值条件更改 noUiSlider 连接颜色?

javascript - 嵌入对象 HTML 的高度

javascript - 迭代数组以获得一个值。

javascript - 在 Protractor 中断言数组列表中值的部分关键字