- Published on
Object-Oriented Programming
Table of Contents
Objects and Prototypes
- In JavaScript, everything is an
objector behaves like one. An object is a collection of key-value pairs where each value can be a primitive data type, an array, or another object. Prototypesin JavaScript are a way of linking objects to other objects. Each object in JavaScript has a prototype, which acts as a fallback mechanism for property access. When you try to access a property on an object, JavaScript first checks if that property exists on the object itself. If it doesn't, it looks for it in the object's prototype, and so on, forming a chain until it reaches the end of the prototype chain.
Example:
// Creating an object using object literal syntax
let person = {
name: 'Kumar',
age: 30,
greet: function () {
console.log('Hello, my name is ' + this.name)
},
}
// Adding a property to the prototype
Object.prototype.sayHello = function () {
console.log('Hello!')
}
person.sayHello() // Output: Hello!
Constructors and Classes
- In JavaScript,
constructorsare functions used for creating objects. They are called with the new keyword to create instances of objects. Classesin JavaScript are primarily syntactic sugar over the existing prototype-based inheritance, introduced in ECMAScript 2015 (ES6).
Example:
// Constructor function
function Person(name, age) {
this.name = name
this.age = age
}
// Adding a method to the prototype
Person.prototype.greet = function () {
console.log('Hello, my name is ' + this.name)
}
// Creating an instance using the constructor function
let person1 = new Person('Kumar', 25)
person1.greet() // Output: Hello, my name is Kumar
Inheritance and Prototype Chain
Inheritance in JavaScript is achieved through prototypes. Each object has a prototype, and when a property or method is not found on an object, JavaScript looks for it in the prototype chain.
Example:
// Inheriting from Person
function Student(name, age, grade) {
Person.call(this, name, age)
this.grade = grade
}
// Setting up prototype chain
Student.prototype = Object.create(Person.prototype)
Student.prototype.constructor = Student
// Adding a method specific to Student
Student.prototype.study = function () {
console.log(this.name + ' is studying.')
}
let student1 = new Student('Kumar', 20, 'A')
student1.greet() // Output: Hello, my name is Kumar
student1.study() // Output: Kumar is studying.
Encapsulation, Abstraction, Polymorphism
Encapsulationis the bundling of data and methods that operate on the data into a single unit, called a class in classical OOP languages.Abstractionrefers tohidingthe complexity and onlyshowingthe necessary details of anobject.Polymorphismrefers to the ability of objects to take on different forms or respond to the same message in different ways. Example:
// Encapsulation and Abstraction
class Animal {
constructor(name) {
this.name = name
}
// Abstraction: hiding the details of movement
move() {
console.log(this.name + ' moves.')
}
}
// Polymorphism
class Dog extends Animal {
constructor(name) {
super(name)
}
// Overriding move method
move() {
console.log(this.name + ' runs.')
}
}
class Bird extends Animal {
constructor(name) {
super(name)
}
// Overriding move method
move() {
console.log(this.name + ' flies.')
}
}
let dog = new Dog('Buddy')
let bird = new Bird('Sparrow')
dog.move() // Output: Buddy runs.
bird.move() // Output: Sparrow flies.