TypeScript Fundamentals Public & Instance Fields
This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.
Transcript from the "Public & Instance Fields" Lesson
[00:00:27] I should really probably just say stage 2 because there's no guarantee when it'll be finalized. But this is the proposed syntax here, where instance fields like this planet Earth I have as an instance property of person. That will be treated just like, that is the equivalent to putting a property on an instance in the constructor.
[00:00:51] So planet Earth here, that is being treated the same way we are treating name more or less. Think of it as in the constructor for each instance we are saying This.planet=earth. It is not the same as placing it on the prototype. Static fields do not require an instance and it’s the equivalent of just tacking something onto a constructor.
[00:01:10] So we should be able to do something like Person._counter and we’d be able to get this value. In this case, we can see that as we create new instances of person that counter is incremented and so that is state that is attached to the whole class as opposed to any given instance.
[00:02:20] We push a tag into the array going through p1, person number 1. And we observe that person number 2 also like seems to have this tag on it. And the reason is, in this situation, we are sharing that same instance of an array tags across all persons that we create.
[00:02:38] It's one array they're all point to it. Because there's only one copy of this prototype in memory. It's sort of the fallback whenever we're asking for properties on an instance of person, and those instances don't have anything for us. We go to the prototype and we see what's there, right?
[00:03:25] So when I say the classes have a way of sending down some of the sharp edges and corners around prototypes, that's what I'm referring to here, right? We don't really have, unless we use the starter keyword, we kinda have to opine to shared multiple state across instances. And sort of that being the default.