JS Intermediate – Object-Oriented Programming(OOP)

Home /

Table of Contents

Introduction to Object-Oriented Programming

Object-oriented programming (OOP) is a type of programming method that allows a programmer to create abstract data types through which programs can be written using features such as emulation, inheritance, and polymorphism. As we know, a program is actually a collection of data and instructions. Writing a program can be either data-oriented or instruction-oriented.

Structured programming is instruction-centric programming. In this programming method, the data is controlled by instructions, but on the other hand, object-oriented programming is data-centric programming. In this programming method, the programmer’s instructions are controlled by the data.

Basically, object-oriented programming is introduced through the basic concepts of Spider:

  • Objects
  • Class
  • Messages
  • Encapsulation
  • Inheritance
  • Polymorphism

Objects: An object or event in the object-oriented programming language is an object’s programming version. For example, a car in the program,

  • model. For example – Toyota Corolla
  • Identification Number. Like- Dhaka T20201
  • Motor types. Eg – 8-Cyl

can be present through etc. In this case, my specific car is an object. An object can have attributes as well as behaviors. Attributes are the means by which the characteristics of the associated object are represented, such as the car’s color. On the other hand, behaviors are means through which the work of the corresponding object can be presented. For example, stopping to drive is a type of behavior.

Object-Oriented JavaScript

JavaScript is actually an Object-Oriented Programming Language (Object-Oriented Programming Language). Although JavaScript is slightly different from other OOP (Object Oriented Programming) languages, it still falls under the category of object-oriented languages. They are also being updated slowly with new versions. And so today, in this episode, we will discuss Object Oriented JavaScript. Almost everything in JavaScript is an object. except for some, which are called primitive data types.

There are generally two types of data types in JavaScript:

  • Primitive Data Type
  • object

Primitive Data Type: There are very few primitive data types in JavaScript. Primitive data types include numbers, strings, booleans, undefined, and null.

Objects: Except for the primitive data types mentioned above, all are objects in JavaScript. For example array, function, object, date, number/string/boolean wrapper (Wrapper) function are all objects.

Now the main difference between primitive data types and objects is in their systems for storing values. A primitive data type stores the value in itself, but an object usually doesn’t store the data directly. Instead, the data is stored somewhere else, and the object just has a reference to it.

And when we pass any data of primitive type as a parameter/argument to a function, it always sends a copy of that data, not direct data.

Think Everything as an Object

Everything which takes multiple properties to describe is an Object.

2UUZ13eUKJeTkw1UPcOzlqmlwbdlQyYuP4kHHpfnu vKEa8gqKj75cWMTioxBxYdI6lG6CIzgRMdawQRReouDsEZ5 avPsQEYXFRRrcBvoInP9sNPHqew akbwfQL2tb6vyBM2Yl siAJ8i1zkI - JS Intermediate - Object-Oriented Programming(OOP)

Look like the number 45. It can be expressed as an Object. If it is written as 45.00. It has an integer part, a fractional part, a boolean value (positive or negative), and a string value. So like this, everything can be expressed as an Object.

Methods and Properties

Methods are the operations that can be performed by an object. The qualities that an object possesses are called Properties.

Example:

var rect = {
    width: 10,  // These are Properties
    height: 20, // These are Properties

    calArea: function () {  // These are methods
        return this.height * this.width
    },

    calCircum: function () {    // These are methods
        return 2 * (this.height + this.width)
    }
}

var area = rect.calArea()
var circumference = rect.calCircum()
console.log(area);
console.log(circumference);

Output:

200
60

Concept of Class

A Class is just a factory to create multiple objects without code duplication. It is a template made up of variables and functions. Sounds like an object, right? More precisely, a class is a blueprint from which specific objects are created. Classes also have attributes and functions that are called methods.

Example:

class Rect {
   constructor (width, height){
   this.width = width;
   this.height = height;
}
   calArea() { 
       return this.height * this.width
   }
   calCircum() {
       return 2 * ( this.height + this.width)
   }
}
 
var rect1 = new Rect(10,20)
var rect2 = new Rect(34,56)
var rect3 = new Rect(45,65)
console.log(rect1);
console.log(rect2);
console.log(rect3);

Output:

Rect { width: 10, height: 20 }
Rect { width: 34, height: 56 }
Rect { width: 45, height: 65 }
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article