Posts

Abstraction

  Abstraction is a fundamental concept in object-oriented programming (OOP) that refers to the practice of hiding the implementation details of an object and exposing only the essential features to the user. In JavaScript, abstraction is achieved by using abstract classes and interfaces. An abstract class is a class that cannot be instantiated and is meant to be used as a base class for other classes. Abstract classes typically contain one or more abstract methods, which are methods that have a signature but no implementation. These methods must be implemented by the derived classes. JavaScript does not support abstract classes natively, but you can achieve similar functionality by using a combination of function constructors and prototypes. For example, consider the following code: function Shape () { if ( this . constructor === Shape) { throw new Error ( "Cannot instantiate abstract class Shape" ); } this . draw = function () { throw new...

Polymorphism

  Polymorphism is a fundamental concept in object-oriented programming (OOP) that refers to the ability of different objects to respond to the same method call in different ways. In JavaScript, polymorphism is achieved by using function overloading and function overriding. Function overloading refers to the ability of a function to have multiple implementations based on the number and/or types of arguments passed to it. JavaScript does not support function overloading natively, however, you can achieve similar functionality by using the arguments object and checking the number and/or types of arguments passed to the function. Function overriding refers to the ability of a subclass to provide a different implementation of a method that is already provided by its superclass. In JavaScript, this can be achieved by reassigning the prototype of the subclass. For example, consider the following code: class Shape { constructor ( name ) { this .name = name; } draw ()...