October 01, 2024

Objects and Object Methods in JavaScript

Introduction to Objects


  • Objects in JavaScript are like the *all-in-one* Swiss Army knife of coding. Need to group related data together? Need a structure that’s flexible, powerful, and easy to use? That’s an object for you! Whether it’s managing a shopping cart, organizing user data, or handling complex configurations, objects are your go-to tools. Think of objects like the family tree of the Kapoor Khandan—each member has unique attributes, but they all belong to the same family.


Creating Objects


  • To create an object, we use curly braces {}. Inside these braces, we define key-value pairs. The key is like the name of a family member (e.g., “firstName”), and the value is like their unique characteristic (e.g., “Ranbir”).


Example:


let ranbir = {
    firstName: "Ranbir",
    lastName: "Kapoor",
    profession: "Actor",
    age: 38,
    movies: ["Barfi", "Yeh Jawaani Hai Deewani", "Sanju"]
};


Explanation: Here, we created an object named ranbir. It has properties like firstName, lastName, profession, age, and movies. Each property (or key) is paired with a value, just like how each Kapoor has a unique trait.


Accessing Object Properties


  • Accessing properties of an object is as easy as grabbing popcorn during a movie. You can do this using dot notation or bracket notation.


Dot Notation:


console.log(ranbir.firstName); // Output: "Ranbir"


Bracket Notation:


console.log(ranbir["movies"]); // Output: ["Barfi", "Yeh Jawaani Hai Deewani", "Sanju"]


Explanation: Dot notation is straightforward—just like saying “Kapoor family’s Ranbir.” Bracket notation is useful when the property name is dynamic or when you want to access it using a variable.


Modifying Object Properties:


  • Want to update an object’s property? It’s as simple as giving a character a new role in the sequel of a movie. Just use dot or bracket notation to reassign the value.


Example:


ranbir.age = 39; // Ranbir celebrated his birthday!
ranbir["profession"] = "Producer"; // Adding a new profession to his resume.


Explanation: Just like how an actor can take on new roles, object properties can be updated or even added on the fly.


Adding and Deleting Properties


  • Adding or deleting properties from an object is like casting new actors or removing them from your movie lineup. You can add a new property by simply assigning a value, or delete an existing one using the delete keyword.


Example:


ranbir.awards = ["Filmfare", "IIFA"];
delete ranbir.age;


Explanation: In this example, we added a new property awards and deleted the age property. Think of it as adding new accolades to Ranbir’s profile while hiding his age (a true Bollywood move).


Object Methods


  • Objects can also have methods, which are functions defined within the object. Think of methods as special abilities of an object, like how *Shaktimaan* could fly or become invisible.


Example:


let ranbir = {
    firstName: "Ranbir",
    lastName: "Kapoor",
    fullName: function () {
        return this.firstName + " " + this.lastName;
    },
    introduce: function () {
        console.log("Hi, I’m " + this.fullName() + " and I’m an actor.");
    }
};

ranbir.introduce(); // Output: "Hi, I’m Ranbir Kapoor and I’m an actor."


Explanation: Here, we defined two methods: fullName and introduce. The introduce method uses the this keyword to refer to the current object (ranbir) and calls the fullName method to return a string that combines the first and last names.


this Keyword:


  • The this keyword is like the main character of a movie—it always refers to the object it's part of. Just like how Shah Rukh Khan in My Name is Khan says “My name is Khan,” this in a method says, “I’m the current object.”


Example:


let myObject = {
    name: "Shah Rukh Khan",
    role: "Actor",
    introduce: function () {
        console.log("Main hoon " + this.name + " and I’m an " + this.role + ".");
    }
};

myObject.introduce(); // Output: "Main hoon Shah Rukh Khan and I’m an Actor."


Explanation: In this example, this.name refers to myObject.name, and this.role refers to myObject.role. this always points to the object the method belongs to.


Conclusion:


  • Objects in JavaScript are versatile and powerful, much like the A-list stars of Bollywood and Hollywood. They can store a wide range of data, perform actions, and evolve over time. Whether you’re a seasoned coder or a newbie just getting started, mastering objects is key to unlocking the full potential of JavaScript. So, keep practicing, and soon you’ll be the Baadshah of JavaScript objects!

JavaScript
Objects
Object Properties
Object Methods