Objects in JavaScript

Objects in JavaScript

Objects:

  • An object is a collection of key-value pairs, where each key is a string or symbol.

  • The associated value can be of any data type, including other objects or functions.

const user = {
    name: "pashuapti",
    "full name": "pashupati bansode",
    age: 23,
    location: "pune",
    email: "pashuapti@gmail.com",
    isLogged: false,
    lastLoginDays: ["monday", "friday"],
    [mysym]: "key2"
}

1. Meet the Singleton Pattern:-

  • Imagine you have a rule: only one special box of a certain kind. That's the Singleton pattern.

  • We use Object.create to make sure there's just one special box.

2. Object Literals - The Easy Way:-

  • Think of object literals as a quick and easy recipe for making a box.

  • it's like saying, "I want a box with these things inside."

3. Symbols - The Unique IDs:-

  • Symbols are like secret names for your boxes. They're unique, preventing mix-ups. Here's a quick peek:
const mysym = Symbol("key1");
const user = {
    // ... other stuff
    [mysym]: "key2"
};

4. Grabbing and Changing Stuff:-

  • Getting things from your box is easy – just ask for them. Want to change something? No problem, just ask nicely and update it.

5. Freeze for Safety:-

  • Ever wanted to make sure no one messes with your box? Use Object.freeze to lock it down. No changes allowed!
Object.freeze(user);

6. Fun with Functions:-

  • You can teach your box to do things with functions. It's like giving it special powers. Check this out:
user.greeting = function() {
    console.log("hello JavaScript fans");
};