For working professionals
For fresh graduates
More
JavaScript Tutorial Concepts -…
1. Introduction to JavaScript
2. JavaScript Examples
3. Applications of JavaScript
4. JavaScript Hello World
5. JavaScript let
6. JavaScript Callbacks
7. JavaScript Arrays
8. JavaScript Array reduce()
9. JavaScript Loops
10. JavaScript Objects
11. DOM Model in JavaScript
12. JavaScript Form Validation
13. JavaScript Games
14. JavaScript Promises
15. JavaScript Async Await
16. JavaScript Closure
17. JavaScript this keyword
18. OOP in JavaScript
Now Reading
19. Operators in Javascript
20. Redirect in JavaScript
21. Express JS
22. Convert String to int in JavaScript
23. JavaScript Functions
24. JavaScript New Lines
25. JavaScript Regex
26. JavaScript Events
Object-oriented programming in JavaScript is one of the types of programming supported by JavaScript. It deals with objects and classes and allows us to write more manageable and reusable code. The OOP in JavaScript concept has helped me time and time again to write efficient and clean code.
This JavaScript object oriented programming tutorial is sure to give you a basic idea about it. I will try to include as many oops concepts in JavaScript with example as I can to give a better understanding.
JavaScript is an object oriented language but what does an object mean? Before we delve deeper into JavaScript object oriented concepts, let me explain what exactly an object is.
In object-oriented programming (OOP), an object is an instance of a class. This means, it represents a real-world entity or concept and includes both data (attributes or properties) and activity (methods or functions) associated with that entity. Say for example if we take a class to be Fruits, an apple can be an object of the class.
Now let me discuss some key features of JavaScript below.
Now let me show a JavaScript OOP example to explain how objects work.
// Class definition
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
}
}
// Object instantiation
let person_1 = new Person('Ali', 32);
let person_2 = new Person('Bobby', 26);
// Using objects and calling methods
person_1.sayHello(); // Output: Hello, my name is Ali and I'm 32 years old.
person_2.sayHello(); // Output: Hello, my name is Bob and I'm 26 years old.
In the above example, both person_1 and person_2 are objects. They both belong to the Person class. Each of them has individual data(name and age) and method(sayHello).
Now that we know what an object is, let me tell you about classes. In object-oriented programming JavaScript, a class is a blueprint or template for object creation. It specifies the properties and methods that objects in that class will have. Classes help to organize and structure code by encapsulating related data and functions into a single unit.
Now let me share with you some features of classes with respect to JavaScript OOP methods.
To understand the concept of classes better, let me share a JavaScript OOP example.
// Class definition
class Fruits {
// Constructor to initialize properties
constructor(name, price) {
this.name = name;
this.price = price;
}
// Method to greet
Fruit_1() {
console.log(`The name of the fruit is ${this.name} and its price is ${this.price} .`);
}
}
// Creating objects using the class
let apple = new Fruits('apple', 120);
let banana = new Fruits('banana', 100);
// Using objects and calling methods
apple.Fruit_1(); // Output:The name of the fruit is apple and its price is 120.
banana.Fruit_1(); // Output:The name of the fruit is banana and its price is 100.
In the above example, I have defined a class called Fruits with properties named name and price. Fruit_1 is a method of the class Fruits. Then we have created two instances of the class called apple and banana with their individual attributes. Finally, we have used the method Fruit_1 on both objects.
Object-oriented programming is a type of programming that uses the concept of objects. The individual objects contain data and methods. There are four main concepts in object-oriented programming namely.
Encapsulation in object-oriented programming includes combining data and methods that interact with the data into a single unit, often an object. This helps to organize and structure code by hiding an object's internal state and offering controlled access to its attributes and operations via interfaces.
Encapsulation has several advantages. Let me explain some of them here.
Inheritance is a fundamental concept in object-oriented programming that enables the reuse of data and methods and facilitates hierarchical relationships between classes, promoting code reusability.
Some features of inheritance are as follows:
Let me share a JavaScipt OOP function to demonstrate inheritance.
Code:
// Parent class or Superclass
class Vehicle {
constructor(make, model) {
this.make = make;
this.model = model;
}
start() {
console.log('Start the vehicle.');
}
}
// Child class or derived class
class Car extends Vehicle {
drive() {
console.log('Drive the car.');
}
}
// Creating instances of classes or object
let myCar = new Car('Fiesta', 'Ford');
myCar.start(); // Inherited method from Vehicle class
myCar.drive(); // Method specific to Car class
In the above example, the Car class inherits the start method from the Vehicle class using inheritance, showing the concept of code reusability and hierarchical relationships in OOP.
The concept of Polymorphism in object-oriented programming means that a program might have different objects that share the same name but show different properties when called in a different context. It allows us to use objects in different classes and access them as if they belong to one superclass.
Let me now discuss some key features of polymorphism here.
Let me show you how polymorphism works with the help of a JavaScript object oriented programming example.
Code:
// Shape superclass with a draw method
class Shape {
draw() {
console.log('Draw a shape.');
}
}
// Square subclass that overrides the draw method in shape class
class Square extends Shape {
draw() {
console.log('Drawing a Square.');
}
}
// Circle subclass that overrides the draw method
class Circle extends Shape {
draw() {
console.log('Drawing a Circle.');
}
}
// Using polymorphism with different shape objects
let shape1 = new Square();
let shape2 = new Circle();
shape1.draw(); // Output: Drawing a Square.
shape2.draw(); // Output: Drawing a Circle.
In this example, both the Square and Circle classes override the draw method inherited from the Shape superclass, exhibiting polymorphic behavior in which the same method (draw) is executed differently depending on the object type (Circle or Square).
As we have learned from this tutorial, the object-oriented programming concept is a core pillar of JavaScript programming. It helps us write clean code and reduces bugs.
This tutorial was aimed to teach the basic JavaScript oops concepts. If you want to learn the more advanced oop in JavaScript concepts, I would recommend checking out courses from upGrad.
Their courses are in collaboration with some of the best universities around the world. The courses are also curated by the best professors in the field.
OOP in JavaScript is a style of programming in which code revolves around objects that include attributes and methods, allowing for efficient and reusable code. Classes, inheritance, encapsulation, and polymorphism are examples of OOP concepts in JavaScript that help to structure and manage code complexity.
Yes, you can build object-oriented programming (OOP) code in JavaScript by employing classes, inheritance, encapsulation, and polymorphism to construct modular, reusable, organized code.
OOP is widely utilized in JavaScript, particularly after the introduction of ES6 and the availability of classes.
The four main concepts of OOP in JavaScript are polymorphism, data abstraction, encapsulation, and inheritance.
JavaScript has only one fundamental object-oriented programming (OOP) method, which includes the use of objects, classes (introduced in ES6), inheritance, encapsulation, and polymorphism.
The fundamental idea behind OOP in JavaScript is to arrange code around objects that contain both methods and attributes. This increases the modularity, and reusability of the code.
The four types of OOP in JavaScript are as follows.
The full form of OOPs is Object Oriented Programming Systems..
The eight types of JavaSCript are as follows.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.