OOP is a widely used concept in programming. Almost all modern languages, except C language, follow OOP principles. The concept is related to classes and objects. Classes are the design or blueprint of an object, which defines the core properties and functions. **_Objects _**are instances of classes, which contains property and methods. JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP).

How do we implement a class and an object in JavaScript?

To make it more understandable, let’s see an example. To create a Movie class, we need movie’s name and director when we initialize the instance. So the Class will look like the following.

class Movie {

    //initialize the instance of Move class with name and director
    constructor(name, director){
       this.name = name
       this.director = director

    }
}

//create a titanic Object with Movie class
let titanic = new Movie('Titanic', 'James Cameron') // Movie { name: 'Titanic', director: 'James Cameron' }

After we define the class, when we create Employee instances, like “john” in the example above, “john” object will have “name” and “id” attributes that we can call to get the result we want: john.name will give us “John” and john.id will show us “001”.

Other than class and object, object oriented programming has four principles:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

#programming #interview #object-oriented #javascript #introduction

Object Oriented Programming in JavaScript
1.35 GEEK