JavaScript Objects
Objects are collections of key-value pairs and are fundamental to JavaScript programming. They allow you to group related data and functionality.
📦 What are Objects?
In JavaScript, an object is a standalone entity with properties and type. It's a collection of key-value pairs where keys are strings (or Symbols) and values can be any data type.
Object Characteristics:
- Unordered collection of key-value pairs
- Keys are unique strings (or Symbols)
- Values can be any data type
- Properties can be added/deleted dynamically
- Support inheritance through prototype chain
💡 Object Creation Methods
// Object literal
let obj1 = {};
// Object constructor
let obj2 = new Object();
// Object.create()
let obj3 = Object.create(null);
// ES6 class
class MyClass {}
let obj4 = new MyClass();Property Access:
Dot notation: obj.property
Bracket notation: obj["property"]
Use brackets for dynamic property names or special characters.
🔧 Creating and Using Objects
Let's explore basic object creation, access, and modification.
Access
obj.key obj["key"]
Add/Update
obj.key = value obj["key"] = value
Delete
delete obj.key
Check
"key" in obj
obj.hasOwnProperty("key")✨ Object Static Methods
JavaScript provides built-in static methods on the Object constructor for common operations.
Enumeration
Object.keys()- Property namesObject.values()- Property valuesObject.entries()- Key-value pairs
Copy/Merge
Object.assign()- Copy/merge{...obj}- Spread operatorJSON.parse(JSON.stringify())- Deep copy
Protection
Object.freeze()- Make immutableObject.seal()- Prevent additionsObject.preventExtensions()- Minimal protection
🏗️ Constructors and Classes
Create multiple objects with similar structure using constructors or ES6 classes.
🎯 Class Features (ES6+):
constructor()- Initialization methodstaticmethods - Called on class, not instancesextends- Class inheritancesuper()- Call parent constructorget/set- Getters and setters- Private fields (
#privateField) - ES2022
🌀 Object Destructuring
Destructuring allows you to extract multiple properties from objects into distinct variables.
Destructuring Features:
- Extract multiple properties at once
- Rename variables during extraction
- Provide default values
- Nested destructuring
- Rest operator in destructuring
Common Use Cases:
- Function parameters
- Importing module exports
- Configuration objects
- API response handling
- React props destructuring
⚡ Object Spread Operator
The spread operator (...) copies enumerable properties from one object to another.
⚠️ Shallow vs Deep Copy:
The spread operator and Object.assign() create shallow copies. Nested objects are shared between original and copy. Use JSON.parse(JSON.stringify(obj))or libraries like Lodash for deep copies.
🎮 Object Practice Exercises
Implement the object manipulation functions below.
💪 Additional Challenges:
- Create a function to flatten nested objects
- Implement object validation with schemas
- Create a simple ORM (Object-Relational Mapping)
- Implement object observability (reactive objects)
- Create a deep merge function that handles arrays and nested objects
📋 Quick Reference
Creation:
- Object literalnew Object()- ConstructorObject.create()- With prototypeclass- ES6 class
Essential Methods:
Object.keys/values/entries- EnumerationObject.assign- Copy/mergeObject.freeze/seal- ProtectionObject.hasOwnProperty- Property check