[Tutorial] Encapsulation & "static" - Object Oriented Programming
Hey everyone!
I've created an in depth video about how encapsulation which is one of the four main pillars of OOP, works in Javascript. We also take a look at the "static" keyword within a class and what it means for us when it comes to the Prototype. There's also a set of exercises you can try that can get quite tricky to see how well you understand the concepts 😊
Encapsulation and "static" concepts: https://youtu.be/WAo7Mx8Nnos
Exercises to practice the concepts: https://youtu.be/dVpSNOGj1uk
I hope you find these helpful and as always, I'd love any feedback and to know if it helped you!
16 comments · 95% upvoted · other discussions
Fun fact about private methods is that they're not inherited through the prototype chain like public methods are. Instead they're added directly to the instance during construction like fields. Because of this, you wouldn't be able to access a private member through
supersince that is reaching up in the prototype chain for a value which doesn't exist there.It is, however, possible for subclass to access a private method through
this. The difficulty is gaining access to the private name used to define the method. Private names are scoped to the class block in which they were defined. So if a subclass wants to have access to a private method defined by its superclass, it would also need to be defined within the scope of that superclass.This scoping of the private name is what makes private members private. Being scoped to the class block prevents anything outside that class block from referring to that private member while at the same time allowing any code within it to have access - even if it is code not part of the class itself.
kap89 2 points · Jan 2023
With one very important difference - while private methods are not a part of the prototype, they are still shared between instances of a class, just like public methods:
Nooder [OP] 2 points · Jan 2023
Hey Kap, very nice addition and clarification! 🥳 It's truly quite amazing all the nuances in JS. It's funny because it makes you realize that it's actually more involved than the more "traditional" OOP langs, because of how everything works through the prototype system 🤓
Nooder [OP] 2 points · Jan 2023
Hey Senocular!
Very nice explanation and good catch! You're right technically the way I explained it as being in the chain is a bit wishy washy. Thinking about it in the context of scope makes way more sense. 😃
I've never seen such an example that really shows how it's truly the scope that matters, nice one - very cool! It's funny how there's so much going on in that example too, like how it has closure hence the access to the scope later 🤓
senocular 2 points · Jan 2023
I wouldn't worry about that. You still explain things very well :) and this little detail is not something many people know about (or need to know about) anyway. Even as kap89 pointed out, the methods are still shared between instances so even though its not happening through prototype the behavior appears very similar.
jack_waugh 1 points · Jan 2023
Complexities like this just reconfirm my determination to never use
classwhen I want a class.