


Khusboo Tayal
JavaScript is an object-oriented programming (OOP) language, and at the heart of this paradigm are objects. Understanding how objects work and how JavaScript uses prototypes is crucial for mastering the language.
An object is a collection of related data and functionality (properties and methods). It allows you to group values (including other objects) and functions (methods) together.
Object literals are the easiest and most commonly used way to create objects.
const person = {
name: "Alice",
age: 25,
isStudent: true,
greet: function() {
console.log("Hello, I am " + this.name);
}
};
console.log(person.name); // Output: Alice
person.greet(); // Output: Hello, I am Alice
This is a more formal way of creating an object.
const car = new Object();
car.brand = "Toyota";
car.model = "Corolla";
car.start = function() {
console.log(this.brand + " " + this.model + " is starting...");
};
console.log(car.brand); // Output: Toyota
car.start(); // Output: Toyota Corolla is starting...Object.create() creates a new object using an existing object as a prototype.
const animal = {
type: "Mammal",
eat: function() {
console.log("Eating...");
}
};
// Creating an object using the animal as a prototype
const dog = Object.create(animal);
dog.breed = "Labrador";
dog.bark = function() {
console.log("Woof!");
};
console.log(dog.type); // Output: Mammal (inherited from animal)
dog.bark(); // Output: Woof!
dog.eat(); // Output: Eating... (inherited from animal)Constructor functions are a traditional way of creating objects and are the foundation of object-oriented JavaScript.
function Student(name, age) {
this.name = name;
this.age = age;
this.introduce = function() {
console.log("Hello, I am " + this.name + " and I am " + this.age + " years old.");
};
}
// Creating an object using constructor
const student1 = new Student("John", 21);
student1.introduce(); // Output: Hello, I am John and I am 21 years old.Classes provide a more structured and cleaner way to create objects in JavaScript.
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
console.log(`Hello, I am ${this.name} and I am ${this.age} years old.`);
}
}
const user1 = new User("Alice", 28);
user1.introduce(); // Output: Hello, I am Alice and I am 28 years old.In JavaScript, every object has an internal property called [[Prototype]] that points to another object, known as its prototype.
function Animal(type) {
this.type = type;
}
// Adding a method to Animal's prototype
Animal.prototype.speak = function() {
console.log(this.type + " is making a sound.");
};
const cat = new Animal("Cat");
cat.speak(); // Output: Cat is making a sound.
console.log(cat.toString()); // Output: [object Object]
JavaScript uses prototypes to achieve inheritance.
function Vehicle(type) {
this.type = type;
}
Vehicle.prototype.start = function() {
console.log(this.type + " is starting...");
};
// Inheriting Vehicle prototype
function Car(brand) {
this.brand = brand;
}
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
const myCar = new Car("Toyota");
myCar.type = "Car";
myCar.start(); // Output: Car is starting...