简体   繁体   English

如何拦截或编辑咖喱函数

[英]How to intercept or edit curried function

I'm new to js and functional programing 我是js和函数式编程的新手

my question is below 我的问题在下面

const orgFunc = (str, str2) =>{
    return str + ' ' + str2;
}

const curryFunc = (str) =>
{
    return orgFunc(str,'curried'); 
}

const interceptFunc = (fn) =>
{
    return fn;
}

1 console.log("orgFunc",orgFunc('func','org'));
>func org
2 console.log("curryFunc", curryFunc('func'));
>func curried    
3 console.log("interceptFunc", interceptFunc(curryFunc)('func'));
>func curried

I want to change 3's result "func curried" to "func intercepted" with replacement "curried" to "intercepted" 我想将3的结果“ func curried”更改为“ func拦截”,并将“ curried”替换为“拦截”

but in interceptFunc function, 但是在interceptFunc函数中

const interceptFunc = (fn) =>
{
    return fn
}

fn is function so I can't use fn.replace("curried","intercepted") fn是函数,所以我不能使用fn.replace(“ curried”,“ intercepted”)

Is there any way? 有什么办法吗?

You can achieve this by modifying your interceptFunc function. 您可以通过修改interceptFunc函数来实现。

Your interceptFunc is not doing anything other than just returning another function. 你的interceptFunc除了返回另一个函数外没有做任何其他事情。

Check the below code snippet in which we return another function which actually acts as an interceptor. 检查下面的代码片段,我们在其中返回另一个实际上充当拦截器的函数。

 const orgFunc = (str, str2) =>{ return str + ' ' + str2; } const curryFunc = (str) => { return orgFunc(str,'curried'); } const interceptFunc = (fn) => { return function(someInput){ return fn(someInput).replace('curried', 'intercepted'); } } console.log(interceptFunc(curryFunc)("func")); 

I'm not sure what you want to do. 我不确定您要做什么。 But you can use replace like that: 但是您可以像这样使用replace:

 const orgFunc = (str, str2) =>{ return str + ' ' + str2; } const curryFunc = (str) => { return orgFunc(str,'curried'); } const interceptFunc = (fn) => { return fn; } console.log("orgFunc",orgFunc('func','org')); console.log("curryFunc", curryFunc('func')); console.log("interceptFunc", interceptFunc(curryFunc)('func').replace('curried', 'intercepted')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM