The Person function above provides almost similar functionality as of class in Java.
Every function has a special property called prototype, this property is an object and often called as prototype object. By default this prototype object has a property called constructor which points back to our function.
We can add new properties to the Class Person using prototype.
Properties added using prototype becomes automatically available to all the instances of the constructor function.
But it has one problem, Reference type properties get shared between instances.
Now that we have understood constructor function and use of the prototype, Let’s understand what is Class keyword.
You might think this is something different than constructor functions and prototypes, well it’s not!
Class is just syntactic sugar over constructor functions, don’t believe? I thought so, let’s have a look at the below example.
Using classes makes your code “cleaner”, “maintainable” and easy to understand for newbies.
If you enjoyed this article, applaud and sharing is highly appreciated.