【问题标题】:what is the difference between using arrow function and not using arrow?(feat.React) [Edited]使用箭头函数和不使用箭头有什么区别?(feat.React)[编辑]
【发布时间】:2021-04-18 12:59:38
【问题描述】:

我有几个例子。


第一

const { window } = props;
const container =
    window !== undefined ? () => window().document.body : undefined;

这意味着如果窗口不是undefined,它将在正文中显示一些内容。
但是使用下面有什么区别??

const container =
    window !== undefined ? window().document.body : undefined;

其次
在 jsx 中,我设置了 onChange 事件,这两者的工作方式不同。

const handleClick = () => { console.log('hi'); }
<input onChange={handleClick} type="text"/> 
//=> this operates well when input tag value is changed.

<input onChange={()=>handleClick} type="text"/>
//=>it doesn't work even when input tag value is changed.
  • 已编辑
    现在我清楚地了解了这些差异。
1. <input onChange={handleClick} type="text"/>
2. <input onChange={() => handleClick()} type="text"/> 
//1,2 work same. But when you have to pass 'event' or some parameters to
//function, you can use second way.

//or If you wanna put more than one functions on `onChage` event,
2-1. <input onChange={() => {handleClick(); handleCheck();}} type="text"/> 

3. <input onChange={handleClick()} type="text"/> 
//This will call function immediately (when component mounted)
// even 'input' tag is not changed.

4. <input onChange={()=> handleClick} type="text"/> 
//This just returns `handleClick` function so `handleClick` will not be operated.




【问题讨论】:

  • 在第一种情况下container 是一个函数,在第二种情况下不是。你没有显示你在哪里使用container,所以很难说哪个是“正确的”——尽管我不认为这是一个明显的功能,因为我怀疑这个值是否会改变(如果它曾经改变过) ,在 React 的上下文中重新渲染,因此 container 的重新计算应该自动发生)。
  • 对于底部的 onChange 示例 - onChange 需要一个函数,该函数(可选)将事件对象作为参数。假设handleClick是这样一个处理函数,那么onChange={handleClick}是正确的,onChange={()=&gt;handleClick}什么也不做,只是返回处理函数,而不调用它。
  • 现在我想我理解了您的一般问题 - 简而言之:如果 a 是某种值,那么 () =&gt; a 是一个不带参数的函数,并且在执行时总是返回 a没有其他的。例如,数字 2 和始终返回 2 的函数之间是有区别的。
  • 哦,我完全明白了!谢谢!!

标签: javascript reactjs arrow-functions


【解决方案1】:

首先,在:

const container =
    window !== undefined ? () => window().document.body : undefined;

container 是一个函数,您可以调用它来获取window().document.body 返回的值。但在:

const container =
    window !== undefined ? window().document.body : undefined;

containerwindow().document.body 返回的值。

没有上下文很难判断哪个是正确的,但主要区别在于您读取window().document.body的值:

const wVal = {
  document: { body: "A" }
};
const window2 = () => wVal;

const now = window2 !== undefined ? window2().document.body : undefined;
const deffered = window2 !== undefined ? () => window2().document.body : undefined;

window2().document.body = "B";

console.log(now);        //=> "A"
console.log(deffered()); //=> "B"

其次,在:

<input onChange={handleClick} type="text"/> 

onChange 事件将触发 handleClick 函数被调用(以事件数据作为参数)。但在:

<input onChange={()=>handleClick} type="text"/>

onChange 事件将触发 ()=&gt;handleClick 箭头函数被调用,该函数返回对 handleClick 函数的引用(不调用它)。

【讨论】:

    【解决方案2】:

    使用函数声明或表达式创建的普通函数是可构造和可调用的。 但是,箭头函数只能调用而不是可构造的,即箭头函数永远不能用作构造函数。 因此,它们永远不能用 new 关键字调用。

    //Normal Function
    var hello;
    
    hello = function() {
      return "Hello World!";
    }
    
    
    
    //Arrow Function
    var hello;
    
    hello = () => {
      return "Hello World!";
    }

    【讨论】:

      【解决方案3】:

      正则函数和箭头函数有多重区别。

      1。语法

      // (param1, param2, paramN) => expression
      // ES5
      var add = function(x, y) {
        return x + y;
      };
      // ES6
      let add = (x, y) => { return x + y };
      
      

      2。参数绑定

      箭头函数没有参数绑定。但是,它们可以访问最近的非箭头父函数的 arguments 对象。

      3。该关键字的使用

      与常规函数不同,箭头函数没有自己的this。箭头函数内的this 的值在函数的整个生命周期内保持不变,并且始终绑定到最近的非箭头父函数中的this 的值。

      通过link了解更多信息

      或者你可以找到更多解释on google

      【讨论】:

        【解决方案4】:

        本文https://dmitripavlutin.com/differences-between-arrow-and-regular-functions/ 将使您更好地理解箭头函数和常规函数,然后您可以将其置于反应原生上下文中。

        其次:输入字段上的第二个 OnChange 事件不起作用,因为您已被取消为函数

        第一个 OnChange 工作,因为您正在调用 handleClick decleared

        【讨论】:

        • 问题不是在问箭头函数和常规函数之间的区别(尽管我最初也从标题中预期它会问这个问题)
        • 我知道问题不在于区别,而是要从更广泛的角度处理问题,首先他必须了解箭头函数和常规函数,然后他才能知道何时以及为什么在 React Native 中使用它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        • 2021-02-01
        • 1970-01-01
        • 2020-05-07
        相关资源
        最近更新 更多