Object Methods
Master JavaScript object methods! Learn Object.assign, Object.freeze, Object.create, Object.fromEntries, hasOwnProperty, advanced destructuring, and more.
Master JavaScript object methods! Learn Object.assign, Object.freeze, Object.create, Object.fromEntries, hasOwnProperty, advanced destructuring, and more. This hands-on tutorial focuses on practical implementation of object methods concepts.
Object Methods Deep Dive ποΈ
This lesson covers the complete toolkit of static Object methods β each briefly explained β plus advanced patterns for working with objects in modern JavaScript.
1. Merging & Copying Methods π
These methods help you combine or copy objects.
| Method | What it does |
|---|---|
| Object.assign(target, ...sources) | Copies own enumerable properties from sources into target. Shallow copy |
| {...obj} (Spread) | Spreads object into a new object literal β cleaner than Object.assign for copies |
| structuredClone(obj) | Creates a deep clone β nested objects are fully copied (built-in, modern) |
2. Conversion Methods π
These methods convert objects to/from other structures.
| Method | What it does |
|---|---|
| Object.keys(obj) | Returns array of the object's own property names |
| Object.values(obj) | Returns array of the object's own property values |
| Object.entries(obj) | Returns array of [key, value] pairs |
| Object.fromEntries(iterable) | Creates an object from [key, value] pairs β reverse of entries |
3. Immutability Methods π
These methods control whether an object can be changed.
| Method | What it does |
|---|---|
| Object.freeze(obj) | Makes the object completely immutable β no adds, updates, or deletes |
| Object.seal(obj) | Prevents adding/deleting but allows updating existing properties |
| Object.isFrozen(obj) | Returns true if the object is frozen |
| Object.isSealed(obj) | Returns true if the object is sealed |
4. Prototype & Inspection Methods π¬
These methods work at the prototype chain level.
| Method | What it does |
|---|---|
| Object.create(proto) | Creates a new object with a specified prototype |
| Object.getPrototypeOf(obj) | Returns the prototype of an object |
| Object.getOwnPropertyNames(obj) | Returns all own property names, including non-enumerable |
| Object.defineProperty(obj, key, descriptor) | Defines a property with fine-grained control over writability, enumerability, etc. |
5. Property Ownership Methods π§ͺ
These check whether a property belongs directly to an object (not inherited).
| Method | What it does |
|---|---|
| Object.hasOwn(obj, key) | Returns true if the object directly has the property β preferred modern approach |
| obj.hasOwnProperty(key) | Same as above but an instance method β can be shadowed, so less safe |
| key in obj | Returns true if key exists anywhere on the prototype chain (includes inherited) |
6. Advanced Destructuring Patterns π―
Destructuring is the modern way to extract values from objects.
| Pattern | What it does |
|---|---|
| const { a, b } = obj | Basic extraction |
| const { a: alias } = obj | Extract and rename to a new variable name |
| const { a = default } = obj | Extract with a default value if property is missing |
| const { a, ...rest } = obj | Extract a and collect everything else into rest |
| const { outer: { inner } } = obj | Destructure nested objects |
Practical Example: API Response Normaliser π
AI Mentor
Confused about "JavaScript object methods: Object.assign, freeze, seal, create, fromEntries, hasOwn, structuredClone and advanced destructuring"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 5What is the difference between Object.freeze() and Object.seal()?
Key Takeaways
β
Merge: Object.assign or spread {...obj} β both shallow.
β
Deep clone: structuredClone(obj) β copies nested objects fully.
β
Convert: Object.entries + Object.fromEntries β transform objects like arrays.
β
Immutability: freeze() (no changes) vs seal() (no add/delete).
β
Ownership: Object.hasOwn β check direct properties safely.
β
Destructuring: rename, defaults, rest, and nested in one step.
What's Next?
You've mastered objects! Next, see how JSON is used to send objects across the web.
Keep coding! π