Object prototypical inheritance is at the heart of JavaScript. Building on what we learned in the previous post about functions, this post goes deeper into functions and the prototypical inheritance. Specifically, we will take a look at the Class function and Factory function.
Class
in JavaScriptJavaScript is a prototype-based and not a class-based language. Since ES6 added a class
declaration to the language, JavaScript developers can create classes with an easier. However, the addition is only syntactical sugar over JavaScript’s prototype inheritance.
In short, JavaScript Class won’t behave as you’d expect in class-based languages. A new()
class does not create a copy to be instantiated. It creates another object with links to the other object’s prototype.
It is crucial to understand that the ES6 Class masks the prototype mechanism under the hood. The two key differences to keep in mind are that:
JavaScript Class property inheritance follows the prototype chain, not the class chain
JavaScript Class properties can be added or removed dynamically at runtime
For more reading on this, check out “Details of the object model.”
#programming #web-development #javascript