简体   繁体   English

这两个ES6功能如何不同地访问“ this”?

[英]How do these two ES6 functions access “this” differently?

I'm curious why functionA returns an error while functionB has a correct reference to "this" and works. 我很好奇,为什么functionA返回错误,而functionB具有对“ this”的正确引用并起作用。 What is the reason for the difference? 差异的原因是什么?

class MyComponent extends Component {
  ...

  render() {
    return (
      <input ref="myInput" type="text" />
      <button onClick={this.functionA} />
      <button onClick={this.functionB} />
    );
  }
}

This throws an error "Can't read property 'refs' of undefined: 这将引发错误“无法读取未定义的属性'refs':

functionA() {
  const val = this.refs.myInput.value;
  console.log(val); 
}

While this correctly shows the value: 尽管这正确显示了值:

functionB = () => {
  const val = this.refs.myInput.value;
  console.log(val); 
}

The difference between the two functions is the use of the arrow expression added on es6 that allow the use of lexycal this. 这两个函数之间的区别是使用了在es6上添加的arrow表达式,该表达式允许使用lexycal this。

Until arrow functions, every new function defined its own this value, preventing you from accessing the correct value. 在使用箭头函数之前,每个新函数都定义了自己的this值,从而阻止您访问正确的值。 One of the common workaround is to define a reference to the this on the correct context level and the use it inside your function: 常见的解决方法之一是在正确的上下文级别定义对此的引用,并在函数中使用它:

var that = this; //creating a reference to the this we will need in a function
that.refs.myInput.value; // in our function we refer to 'that' variable

Arrow functions instead capture the this value of the enclosing context, so the this you are referring in FunctionB is actually taken from the "correct" context. 箭头函数会捕获封闭上下文的this值,因此您在FunctionB中引用的this实际上是从“正确”上下文中获取的。

for more information please check: https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions/Arrow_functions 有关更多信息,请检查: https : //developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions/Arrow_functions

In JavaScript function context is defined while calling the function, not while defining it. 在JavaScript函数上下文是在调用函数时定义的,而不是在定义函数时定义的。

You pass both function as callbacks to other component. 您将两个函数都作为回调传递给其他组件。 They are called outside of object they are defined. 它们在定义的对象之外被称为。 Thus, they do not share it's class context. 因此,他们不共享它的类上下文。

What makes difference here is definition of functionB . 在这里与众不同的是functionB定义。 Using arrow function binds current context to underlying function. 使用箭头功能可将当前上下文绑定到基础功能。

You can read more here http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/ ... or google more if you are interested. 您可以在此处阅读更多信息http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/ ...或如果您有兴趣,可以在google上阅读更多内容。

In traditional function declaration ie function keyword this inside function scope refer to the object to which the function belong , or undefined if there is no hosting object .The point is function form a new scope different from outside 在传统的函数声明即function关键字this内部函数范围是指以该函数所属的对象,或undefined如果没有托管对象.The点是function形成从外部不同的新的范围

In new => syntax ,there is a different story .the function do not from the new scope (quite different the old way) ,rather,its scope is the same as the outside scope 在new =>语法中,存在一个不同的故事。该函数不是来自新作用域(与旧方法完全不同),而是其作用域与外部作用域相同

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

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

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