What Is Object-Oriented Programming in Javascript

Course- Javascript >

As your programs become more complex, you need to use coding techniques that help you to maintain control and ensure that your code remains efficient, readable, and maintainable. In this section you learn the basics of object-oriented programming (OOP), an important technique for writing clear and reliable code that you can reuse over and over.

What Is Object-Oriented Programming?

The code examples to date have been so-called procedural programming. Procedural programming is characterized by having data stored in variables, which are operated on by lists of instructions. Each instruction (or list of instructions, such as a function) can create, destroy, or modify the data, yet the data always remains somehow “separate” from the program code.

In object-oriented programming (OOP), the program instructions and the data on which they operate are more intertwined. OOP is a way of conceptualizing a program’s data into discrete “things” referred to as objects—each having its own properties (data) and methods (instructions).

Suppose, for example, you wanted to write a script to help manage a car rental business. You might design a general-purpose object called Car. The Car object would have certain properties (color, year, odometerReading, and make) and perhaps a few methods (e.g., a method setOdometer(newMiles) to update the odometerReading property to the current figure newMiles).

For each car in the rental fleet, you would create an instance of the Car object.

Writing OOP code offers several advantages over procedural methods:

  • Code reuse—First, OOP allows you to reuse your code in a variety of scripts. You could achieve this with regular functions, but it would soon become difficult to keep track of all the variables that needed to be passed, their scope, and meaning. For objects, in contrast, you only need to document the properties and methods for each object. Providing they adhere to these rules, other programs— and even other programmers—can easily use your object definitions.
  • Encapsulation—You can define the way objects interact with other parts of your scripts by carefully controlling the properties and methods that the rest of the program can “see.” The internal workings of the object can be hidden away, forcing code external to the object to access that object’s data only through the documented interfaces that the object offers.
  • Inheritance—Often when coding you will have a need for some code that is nearly, but not quite, the same as something that’s been coded before—maybe even something already coded in the same application. Using inheritance, new objects can be created based on the design of previously defined objects, optionally with additions or modifications to their methods and properties; the new object inherits properties and methods from the old.

In the previous section you often used objects; either those built into JavaScript, or those that make up the DOM. However, you can also create your own objects, with their own properties and functions, to use in your programs.