gpt4 book ai didi

javascript - 你如何在 Javascript ES6 中取消 curry /反向 curry ?

转载 作者:行者123 更新时间:2023-11-30 11:33:28 25 4
gpt4 key购买 nike

我有一个函数,我必须包装另一个函数(如果它存在)或替换它(如果它不存在)。这意味着参数的数量会根据情况而变化。

这就是我的意思-

列列表:

const myColumns = [
{name: 'My Field', formatter: function(row, cell, value, colDef, data){
... do stuff to value...
return value;
},
{name: 'My Other Field'}
]

有些有格式化程序,有些没有。这是我“包装”函数的部分解决方案:

return myColumns.map(columns => {
function wrapFormatting(value){
return `<span class="foobar">${value}</value>`
}
if( column.formatters ) {
column.formatters = _.flow(column.formatter, wrapFormatting);
} else {
column.formatter = (row, cell, value) => value => wrapFormatting;
}
})

我天真的期望是,在 else block 格式化程序中,我们“反转 curry”/uncurry 三个参数,它最终看起来像这样:

column.formatter = function(row, cell, value){
wrapFormatting(value);
}

但是 eslint 告诉我值是 a) 已经在上层作用域中声明,b) 已定义但从未使用过。如果我更改其中一个(所以我有 (row, cell, value) => val => wrapFormatting 我将它们都定义为“已定义但从未使用过”。

我觉得我错过了一个技巧,因为我不能让 (row, cell, value) => value => wrapFormatting(value) 因为它会立即调用该函数,并且在调用 column.formatter 时不会调用它。

最佳答案

你在找

column.formatter = (row, cell, value) => wrapFormatting(value);

不确定为什么您认为您将 value 作为参数两次并且从不调用 wrapFormatting 而是返回它。

关于javascript - 你如何在 Javascript ES6 中取消 curry /反向 curry ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45377884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com