Chapter 10. JavaScript Object-Oriented Programming

JavaScript is an object-oriented programming language. In object-oriented programming (OOP) languages, we use the concept of objects rather than actions to develop applications. In the past, JavaScript had no real foundation and was just a basic language. JavaScript is not a fully OOP-based language like JAVA, C#, and other programming languages, but it still has many OOP features.

There are many features in JavaScript where code is reused. So, rather than using procedural concepts in JavaScript, we use object-oriented programming techniques. There are four basic principles of object-oriented programming.

Polymorphism

Since JavaScript is a dynamic language, it supports polymorphism. Polymorphism can be understood as the ability of an object to be different at different times. For example, a shape can be a square, a rectangle, or a circle.

Encapsulation

This feature is also supported in JavaScript. It means protecting parts of code from external use. It protects part of the code that does not concern the end user but is important for running an application, such as in an application that stores passwords. Users don't have to know how their passwords are encrypted. Hence, this code is encapsulated.

Inheritance

In JavaScript, inheritance can be used to derive properties of parent objects to their child objects and have some unique attributes for themselves as well. For example, a square and a triangle may inherit their stroke or fill from a shape object and, at the same time, have a number of vertices unique to themselves.

Abstraction

Abstraction is not natively supported in JavaScript, but there are methods through which this can be achieved, using a combination of polymorphism and inheritance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.16