Why cons? JavaScript supports object-oriented programming, but not in the traditional, class-based way you see, for example. Java
How JavaScript OOP works is commonly called "prototype inheritance", or more specifically, "delegated prototype inheritance." This can be reduced to "if you are looking for the" foo "property in the object and you cannot find it, then try looking for" foo "inside the prototype of the object." That is, the property search is passed to the prototype of the object (we say that the object inherits the properties from the prototype).
The use of prototype inheritance has several consequences. For instance:
- Objects in JavaScript are “dynamic” in the sense that they are just a bunch of name-value pairs. New properties can be added and removed at run time, so they are much less "static" than objects in the typical sense of a class.
- How delegated prototype inheritance works ("if you can't find a prototype here, then look here") means that it is much simpler than classic OOP. From a purely prototype point of view, for example, you do not need constructors. "Methods" are simply regular functions that attach to the prototype (which means that they are available as properties for all inherited objects).
There is pro and con with prototype and classic inheritance, but it is important to remember that they are different.
Now JavaScript in some ways is inspired by Java and other classic languages, so they decided to make the syntax "class-like" to make it easier for people accustomed to classes to get started. I will not dwell on this in detail; it is largely documented elsewhere.
Some interesting posts:
source share