功能调用方式之间的区别
我注意到(至少在React中)有不同的方式调用功能。我会说:
onClick={myFunction}
onClick={myFunction()}
onClick={()=>myFunction}
onClick={()=>myFunction()} /*Not sure if I've seen this type*/
这一切都是正确的,它们之间有什么区别?
I have noticed that (at least in React) there are different ways to call a function. I'd say:
onClick={myFunction}
onClick={myFunction()}
onClick={()=>myFunction}
onClick={()=>myFunction()} /*Not sure if I've seen this type*/
Being the case all are correct, what are the difference between them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分配
myFunction
OnClick
。调用
myFunction
立即将返回值(需要是另一个函数)分配给onclick
。这通常是由于人们不了解
()
工作并想要上一个语法而导致的错误。创建一个新功能并将其分配给
onClick
。新功能提到
myFunction
,但对此无能为力。它是 noop 。这总是一个错误。
创建一个新功能,该功能调用
myFunction
,并将其分配给onclick
。这通常是浪费资源。仅当
myFunction
会对事件对象做些onclick
在调用时传递给事件处理程序的事件对象,这是有用的。如果要将参数传递给
myFunction
(此示例没有),那么它将更加有用。Assigns
myFunction
toonClick
.Calls
myFunction
immediately and assigns the return value (which needs to be another function) toonClick
.This is often an error caused by people not understanding how
()
works and wanting the previous syntax.Creates a new function and assigns it to
onClick
.The new function mentions
myFunction
but doesn't do anything with it. It is a noop.It is always a mistake.
Creates a new function, which calls
myFunction
, and assigns it toonClick
.This is usually a waste of resources. It is useful only if
myFunction
would do something with the event object thatonClick
passes to the event handler when called and you want to prevent that.If you were to pass arguments to
myFunction
(which this example does not) then it would be more generally useful.