In JavaScript, OOPs is based on the concept of prototypes, which is different from class-based OOPs in other languages like Java and C++.
In JavaScript, every object has a prototype object, which acts as a template for the object. When a property or method is accessed on an object, JavaScript first looks for that property or method on the object itself. If it's not found, JavaScript looks for it on the object's prototype. If it's still not found, JavaScript continues up the prototype chain until it reaches the Object.prototype, which is the root of the prototype chain.
To create an object in JavaScript, we can use either the object literal notation or the Object() constructor. For example, here's how we can create an object using the object literal notation:
const person = {
name: 'Alice',
age: 30,
gender: 'female',
walk() {
console.log(`${this.name} is walking.`);
},
talk() {
console.log(`${this.name} is talking.`);
}
};
In this example, we create an object called "person" that has properties for the person's name, age, and gender, as well as methods for walking and talking.
We can also create objects using constructor functions, which act as blueprints for creating objects with shared properties and methods. For example:
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Person.prototype.walk = function() {
console.log(`${this.name} is walking.`);
};
Person.prototype.talk = function() {
console.log(`${this.name} is talking.`);
};
const person1 = new Person('Alice', 30, 'female');
const person2 = new Person('Bob', 40, 'male');
person1.walk(); // logs "Alice is walking."
person2.talk(); // logs "Bob is talking."
In this example, we define a constructor function called "Person" that takes in arguments for the person's name, age, and gender, and sets those properties on the newly created object using this. We then add the walk() and talk() methods to the Person.prototype object, which will be shared by all instances of the "Person" object.
Overall, JavaScript's approach to OOPs is based on prototypes rather than classes, which can take some getting used to if you are coming from a class-based OOPs language. However, once you understand the concept of prototypes and how they work, you can create powerful and flexible object-oriented code in JavaScript.