briefly introduces: the this in arrow function points to the function defined by general function, and the definition of arrow function this is that this in arrow function is bound when defining function, rather than binding when executing function.

(1) this points in the implementation of general function is bound when running obj.say (this), refers to the object obj.

 var x=11; VAR obj={x:22, say:function () {console.log (this.x)} {} (obj.say); the output of the output is "2", the so-called definition of time binding, that is, inherit from the father execution context! In the this, such as the arrow here in the this.x function, the arrow function itself and level say in the form of key:value, is also the object of arrow function itself where the obj and obj, the father of the execution context is window, so the this.x actually said is window.x, so the output is 11. 

 var x=11; VAR obj={x:22, say: (=> console.log) {(this.x)};} (obj.say); / / output value of 11

is similar to the

(3):

 var a=11 function test1 (let) {this.a=22; b=function (console.log) {(this.a)}; B (VaR); x=new test1;}); 

(

11 output function:

 var arrow test2 (a=11; function) {this.a=22; let (b=) => {console.log (this.a) B (VaR);}} x=new (test2); / / 22

output is very strange right, I understand, the specific meaning is defined in ES6 when Binding this, the parent should inherit the execution context inside this, is not the father execution context!!! In this way, the direction of many arrow functions is not clear.

note: simple objects (non functions) do not execute the context!

this

understanding the function of the function arrow arrow, pointing to the immobilized this, and not because of the internal mechanism of arrow function binding this, the real reason is that the arrow function did not own this, resulting in internal this is the outer block of code this. It is because it has no this, so it can't be used as a constructor.

we can simulate the arrow ES5 function in Transformation:

 / / ES6 function (foo) {setTimeout (=> console.log) {(('id:', this.id);}, 100);} / / ES5 function foo (VaR) {_this = this; setTimeout (function () {console.log ('id:'. _this.id);}, 100);}

, so when the object is defined, the definition of object attributes, which is generally referred to by this global, or the object is located in the this environment.

the above summary is Xiaobian to introduce the ES6 arrow function in this, I hope to help you, if you have any questions please give me a message, Xiao Bian will reply you timely. Thank you for your support for the home website of the script!

This paper fixed link:http://www.script-home.com/on-the-this-problem-in-the-es6-arrowhead-function.html | Script Home | +Copy Link

Article reprint please specify:On the this problem in the ES6 arrowhead function | Script Home

You may also be interested in these articles!