Again, that will only introduce just a new syntax sugar in the language. In this post we will learn how the classes
actually work under the hood and how we can implement
static properties and
Let us take a look at the ES5 classes. Nothing special but just plane class format as in most of the languages.
Let us see how we could have implemented this same class before es5 came and learn how the classes works under the hood.
So in the below code you will see implementation of the same
foo class from above using a function.
Try running both of these code snippets above and you'll get the same output. This is how you can achieve writing a class implementation supporting old browsers, without using es5 classes.
provides nothing like
The data members or member functions of an object are all public. Any other part of the code can modify or delete them. There are two ways to add public data member and member function in an object. The first way is to create it in the constructor fucntion.
public data member is as easy as that. We use this method of creating public data members through inside
the constructor functions most of the time to initialise data for the new object. The second way to create public data
members is to attach it to the contructor's prototype. This is used to add member functions for inheritance while conserving
memory. Any property which is not available at the object is checked in the prototype chain.
Well this is something new we have to learn. ES5 classes do not provide functionality of
private data members or
member functions. Data abstraction is a key phenomenon of object oriented programming. But it is not something
private data members.
Any variable declared inside the constructor functions, also including the constructor's params are private to the scope
of the constructor function. In the above code snippet,
battery is the private data member of
range are privileged member functions, they have access to the private
battery property and can change
it. But you cannot do that from outside of the
Try tweaking the code, may be making your car more effecient, well I am happy with the 1000kms range.
Now lets learn how we can implement
static data members in functional classes. The
static data members belong to the
constructor function rather than the objects. Let us build a simple objects counter.
Though not with es5 but you can implement static data members using the class syntax in es6. In this post we learned how
we can implement
You can ask your questions or submit feedback in the comments. Also do not forget to try the functional inline code editor and compiler in this post.
By Naman Kumar on 2019-01-10
2018 Year in Review
Each one of us have some goals to complete, things to learn, and places to visit. With the year…
3 Mins Read#yearreview#reading#goals
An undergrad's experience with cracking 8 software engineer intern interviews
If you are graduated or in senior year of your college, you surely can resonate to the fact that…