简体   繁体   English

箭头函数和此函数在构造函数中

[英]Arrow function and this inside a constructor function

I have read this paragraph about the this keyword : https://bonsaiden.github.io/JavaScript-Garden/#function.this 我已经阅读了有关this关键字的这一段: https : //bonsaiden.github.io/JavaScript-Garden/#function.this

In this first case this refers to global objet, and it seems totally normal because when we have an arrow function, it automatically bind this with the one in the outer scope. 在第一种情况下, this是指global对象,这似乎是完全正常的,因为当我们具有箭头功能时,它将自动this与外部作用域绑定在一起。

 var obj = { foo : () => console.log(this) } console.log(obj); obj.foo() 

However, I'm not able to explain the following behavior : 但是,我无法解释以下行为:

 function bar(){ this.foo = () => console.log(this) } var obj = new bar() console.log(obj); obj.foo() 

Now, this refers to obj instead of global . 现在, this是指obj而不是global Why is that ? 这是为什么 ? It seems to me that using the new keyword with the constructor function should return an object obj which is exactly identical as the one in the first example. 在我看来,在构造函数中使用new关键字应该返回一个对象obj ,它与第一个示例中的对象完全相同。 And so the arrow function should have a this which refers to global and not to obj . 因此,箭头函数应该有一个this ,它引用global而不是obj Could you explain to me what's happening in the second case please ? 您能给我解释一下第二种情况吗?

Functions -> No separate this 功能-> 没有单独的this

Until arrow functions, every new function defined its own this value ( a new object in the case of a constructor , undefined in strict mode function calls, the base object if the function is called as an "object method", etc.). 直到箭头的功能,每一个新的函数来定义其自己的这个值( 在一个构造的情况下,一个新的对象 ,未定义在严格模式的函数调用,基本对象,如果该函数被称为“对象方法”,等)。 This proved to be less than ideal with an object-oriented style of programming 事实证明,使用面向对象的编程风格并不理想

Read more about the new keyword here 在此处详细了解new关键字

The constructor function ... is called with the specified arguments, and with this bound to the newly created object . 构造函数...被调用时指定的参数,以及与this绑定到新创建的对象

The bar() constructor defines this as itself. 杆()构造定义this作为本身。

Although I myself am far from good with objects (need to work on that), I think that when you do const test = new Test() you initialize a new object. 尽管我本人对对象(需要进行处理)还const test = new Test() ,但我认为当执行const test = new Test()您会初始化一个新对象。 That way, the scope(?) of this references only to the newly created object, thus why you do this.foo and later on you reference to that foo property by using obj.foo . 通过这种方式,范围(?) this只引用新创建的对象,所以你为什么this.foo ,后来通过使用引用到FOO财产obj.foo On the other hand, when you just do const obj = { .... } , you don't actually initialize a new object, so the this goes straight to the global object - the window. 另一方面,当您只执行const obj = { .... } ,实际上并没有初始化新对象,因此this直接进入了全局对象-窗口。 So, const test = new Test creates a new object, while using only const obj = {} is not a new object and this by default goes to the window object. 因此, const test = new Test创建一个对象,而只使用const obj = {}不是一个新的对象, this在默认情况下进入到窗口对象。

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

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