Class-based vs Prototype-based Programming using JavaScript
Douglas Crockford described JavaScript as the world’s most misunderstood language. Like many developers, when I started learning JavaScript, to me also, this was not a “proper” language. But I quickly understood that JavaScript comes with a rich system of object-oriented programming.There are many concepts that makes JavaScript unique, but prototype model is definitely the most important one and it creates many confusions. So having a clear understanding of prototype model is very important. And in this post, I would like to explain the concept of prototype based programming in JavaScript.The prototype based programming is a very easy to understand concept, but the source of confusion is the syntax. When a new developer looks at the JavaScript code and sees new, class or constructor it makes them think that object instantiation works the same way as other languages. But in reality, they are nothing more than syntactic sugar in JavaScript. Behind the scenes, things are working in different way. So it is very important to have a clear understanding how things works which will help you to predict their behavior.Class Based LanguageA class based language (Java, C++, C# etc.) has two important concepts:class – the interface which defines the properties and methods.entities – instantiations of the class (objects) An instance is exact same copy of the class, you cannot dynamically add/remove properties/methods from the instance as declared in the class. You...
Read More