【问题标题】:difference in function call函数调用的区别
【发布时间】:2018-08-03 19:44:51
【问题描述】:

当我们在 React 中使用它时,我试图了解各个函数调用是如何工作的

假设我们有两个这样的函数

const something =  () => {
//something 
} 

function something1 () {
//something 
}

到目前为止,我已经看到它们以三种不同的方式被调用

喜欢

{this.something}
this.something()
(() => something)

所以我的主要问题是它们有何不同?比如被叫什么时候,有没有其他方法可以叫他们(这里没有提到)?

【问题讨论】:

  • super 是关键字,不能用作标识符。 {this.something}(() => something)都没有调用函数,第一个又是无效语法。
  • 您是 JavaScript 新手吗?你确定你应该在学习语言基础之前直接进入 react 吗?
  • {this.something} 使用 JSX javascript 代码转义语法(即花括号)传递 this.something 引用的任何内容; this.something()调用this.something引用的函数; (() => something) 是一个箭头函数,执行时返回 something 引用的任何内容。也许this tutorial会对你有所帮助。
  • @ASDFGerte {this.something} 在 JSX 中用于转义代码。
  • 这取决于你想要做什么,这个问题可能是相关的,stackoverflow.com/questions/45881670/…

标签: javascript reactjs


【解决方案1】:

this.something 不会正确调用函数。 this.something() 是调用它的理想方式,因为它的性能最高! () => this.something() 实际上在每次调用函数时都会触发重新渲染,因为您正在创建一个新函数(您实际上是在将 this.something() 包装在一个空箭头函数中)。话虽如此,两者在不同的情况下都有用。如有疑问,请使用this.something()

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多