What are objects?

This lesson introduces objects and explains what object properties are in detail using an example.

We'll cover the following

Introduction

In order to understand OOP in JavaScript, it is crucial to get familiarized with the concept of objects.

Objects are a very primary part of JavaScript as almost everything in it is an object. For example functions, arrays, regular expressions, dates and even data types like boolean and strings, if declared with the keyword new, can be considered an object.

What Is an Object?

In real life objects are found everywhere, so these real-life sceneries can also be mapped into object-oriented code.

Example

Let’s discuss this by looking at an example. Assume you have three shapes square, rectangle and circle, which can be considered real-life objects. Now, you want to calculate the areas of all three shapes. In real life what you’d do is draw these shapes on a piece of paper first before calculating their areas.

Now if you were to write a piece of code that would calculate the areas of these three shapes, what would you do? A common approach would be to write functions for all three shapes that would compute the respective areas.

Another approach would be to convert our code to the object-oriented paradigm by creating objects of each one of the three shapes: square, rectangle, and circle. Here, each object has its own set of properties which include:

  • data values
  • functions

So in our example what are the values needed to calculate the areas of the three shapes?

We need the length, width, and the radius. These values will be encapsulated in the object of that particular shape.

Similarly, a function to calculate the area will also be required. This will also be encapsulated in the object as a part of its properties.


Now that you know what objects are, in the next lesson let’s discuss what object literals are and how to create them.