Objects in Node.js
Master JavaScript objects: creation, access, mutation, traversal, comparison, cloning, destructuring, and real-world usage.
Introduction to Objects
Objects are collections of key-value pairs used to group related data and behavior together.
1. What is an Object?
Key-value data structure.
Creating Objects
JavaScript allows object creation using literals, constructors, and factory functions.
1. Object Literal
Most common and recommended.
2. Object Constructor
Using new Object().
Accessing & Updating Properties
Object properties can be accessed using dot or bracket notation.
1. Dot Notation
Access using dot.
2. Bracket Notation
Access using strings.
3. Updating Properties
Modify existing values.
Adding & Deleting Properties
Objects can grow or shrink dynamically.
1. Adding Properties
Add new key-value pair.
2. Deleting Properties
Remove a key.
Traversing Objects
Objects are traversed using Object utilities.
1. for...in Loop
Iterate keys.
2. Object.entries
Key-value pairs.
Comparing Objects
Objects are compared by reference, not by value.
1. Reference Comparison
=== checks memory reference.
2. Value Comparison using JSON
Simple deep comparison.
Cloning Objects
Cloning prevents accidental mutations.
1. Shallow Clone
Using spread operator.
2. Deep Clone
Using structuredClone.
Object Destructuring
Destructuring allows concise extraction of properties.
1. Basic Destructuring
Extract values.
2. Renaming Properties
Alias keys.