-
Notifications
You must be signed in to change notification settings - Fork 113
Context of "this" in properties definition #62
Comments
UPD: @kovensky said
So it's not a bug but just an ambiguous behavior? |
Here is the (abridged) chain of events for calling
It's a bit more complicated when calling
|
What is happening on your example, and what feels like it is ambiguous, is that the superclass' constructor will run to completion before any of your fields are initialized. Because, instead of your own constructor, it is the superclass that is assigning to If you did the assignment on your own constructor on both cases, the result would be identical. |
@Kovensky for me overriding
|
Forbidding the use of The value of |
A real world example where reading from export default class ExpandableParagraph extends React.Component {
state = {
expanded: this.props.defaultExpanded || false
}
// ...
} This allows you to correctly initialize |
@Kovensky changing context for things in arrow functions is straight. class A {
state = { value: this.value }
} must be referred to |
@Kovensky Now I understand how does it works and how can we use it |
I see now what your confusion were w.r.t. what This was previously discussed in tc39/proposal-class-public-fields#34; the consensus is that it should be the object being currently constructed. |
@Kovensky thanks! |
Thanks for the great descriptions @Kovensky and for raising the issue in the first place @syabro . We did discuss the |
I've created a bug here babel/babel#6977 about ambiguous behavior of
this
in property definition, but looks like it could be not a bug.Provide an example code here
will produce
I can't find anything about context for
this
in https://tc39.github.io/proposal-class-fields/Am I missing something? What's the expected behavior? For me using
this
in class properties must be restricted.The text was updated successfully, but these errors were encountered: