DEV Community

Cover image for JavaScript Objects: A Quirky Bunch of Code Characters 🎭
Vipin Sharma
Vipin Sharma

Posted on

JavaScript Objects: A Quirky Bunch of Code Characters 🎭

Welcome to the chaotic yet fascinating universe of JavaScript objects! If you think of them as simple data containers, think again. JavaScript objects are more like an unpredictable group of roommates—sometimes helpful, sometimes messy, and always full of surprises. Let’s explore their quirks with some fun and practical JavaScript coding examples!

1. The Basic JavaScript Object – The Obvious Roommate 😴

const predictablePerson = {
    name: "Bob",
    favoriteFood: "Plain toast",
    hobby: "Filing taxes on time"
};

console.log(predictablePerson.name); // Output: Bob
Enter fullscreen mode Exit fullscreen mode

Ah, Bob. The kind of guy who follows the rules, never forgets a deadline, and thinks "fun" is reading the entire terms and conditions document.

✅ DO:

  • Use objects to logically group related data.
  • Declare objects with const to avoid accidental changes.
  • Keep property names clear and meaningful.

❌ DON'T:

  • Don’t mutate objects unnecessarily—JavaScript has a long memory.
  • Don’t create excessively complex objects unless absolutely needed.

2. JavaScript Object with Methods – The Entertainer 🤹‍♂️

const funnyPerson = {
    name: "Eddie",
    favoritePrank: "Whoopee cushion",
    pullPrank() {
        return `Haha! Gotcha with a ${this.favoritePrank}!`;
    }
};

console.log(funnyPerson.pullPrank()); // Output: Haha! Gotcha with a Whoopee cushion!
Enter fullscreen mode Exit fullscreen mode

Objects with methods are like that one friend who always has a new trick up their sleeve. 🎭

✅ DO:

  • Use methods to encapsulate behavior inside objects.
  • Use the shorthand syntax for cleaner function definitions.

❌ DON'T:

  • Don’t define functions outside an object if they belong inside it.
  • Don’t forget how this works—it can be unpredictable if misused.

3. JavaScript Objects Are Reference Types – The Evil Twin 👿

let original = { mood: "chill" };
let duplicate = original;
duplicate.mood = "stressed";

console.log(original.mood); // Output: stressed
Enter fullscreen mode Exit fullscreen mode

Surprise! JavaScript objects are reference types, meaning if you update duplicate, original changes too. It's like your evil twin taking over your social media account and posting weird selfies.

✅ DO:

  • Use { ...object } (spread operator) or Object.assign({}, object) to create independent copies.
  • Use structuredClone(object) for deep copying.

❌ DON'T:

  • Don’t assume let duplicate = original; makes a fresh copy—it’s just another name for the same object.

4. The Mystery of this in JavaScript Objects – The Lost Tourist 🧭

const lostPerson = {
    name: "Chris",
    askWhereAmI() {
        console.log(this);
    }
};

lostPerson.askWhereAmI(); // Output: { name: "Chris", askWhereAmI: [Function] }

const misplacedFunction = lostPerson.askWhereAmI;
misplacedFunction(); // Output: Window (or undefined in strict mode)
Enter fullscreen mode Exit fullscreen mode

JavaScript’s this is like a tourist who forgot their map—it doesn’t always know where it belongs.

✅ DO:

  • Use .bind(this) when passing functions around.
  • Use arrow functions to avoid losing this.

❌ DON'T:

  • Don’t assume this always points to the object—it changes depending on how the function is called.

Top comments (0)