与内联javascript函数的行为异常不同
在内联javascript函数之后,我看到了不同的行为,我不明白为什么 考虑:与内联javascript函数的行为异常不同,javascript,reactjs,redux-form,Javascript,Reactjs,Redux Form,在内联javascript函数之后,我看到了不同的行为,我不明白为什么 考虑: const someFunc = someParam1 => someParam2 => { // doSomething } const someFuncIndirect = someParam2 => { return someFunc(null)(someParam2) } // I expected parameter1 and parameter2 to be intercha
const someFunc = someParam1 => someParam2 => {
// doSomething
}
const someFuncIndirect = someParam2 => {
return someFunc(null)(someParam2)
}
// I expected parameter1 and parameter2 to be interchangeable but they are not
const funcRef1 = someFunc(null)
const funcRef2 = someFuncIndirect
这里可以看到不同行为的一个例子:
第一个输入框使用内联版本,有一个行为怪癖,即在输入第一个字符后取消输入框的焦点。第二个输入框使用间接方法,不存在该问题。我还注意到,如果我先在第二个输入框中输入文本,第一个输入框上的焦点问题就会消失
所有与函数引用相关的redux表单都是使用它调用React.createElement。父调用方在两者之间是不同的,但我想不出这将如何导致这种行为。我错过了什么
更新:修复了使用someParam2的间接函数示例。在上面粘贴的代码中,
参数1
将是一个接受参数2
并将doSomething
的函数<另一方面,code>parameter2将是一个函数,调用时将返回someFunc
的返回值,该值与parameter1
相同。因此,如果希望两个参数
的行为相同,请将someFuncIndirect
替换为someFuncIndirect()
行中的const parameter2=someFuncIndirect
呃,不能再评论我以前的评论了。这是一个更好的版本:我修复了一个问题中的一个输入错误,其中someFuncIndirect没有使用someParam2
,并更改了函数引用的名称。如果我这样做:funcRef1('test');funcRef2('test')
结果是否应该相同funcRef1
是一个函数,当使用'test'
调用时,该函数将doSomething
。对于funcRef2
,也应如此;当使用'test'
调用时,它将执行someFuncIndirect
,它将使用null
和test
执行'someFunc',这两个参数与funcRef1
传递的参数相同