What are Object methods and how to use them
· Category: JavaScript
Short answer
Object provides static methods to inspect, copy, seal, freeze, and define properties on objects, giving you fine-grained control over structure and mutability.
Steps
- List keys, values, and entries:
javascript Object.keys(obj); // array of keys Object.values(obj); // array of values Object.entries(obj); // array of [key, value] - Merge objects:
javascript Object.assign(target, source1, source2); - Prevent modifications:
javascript Object.freeze(obj); // no changes at all Object.seal(obj); // allow value changes, no add/remove Object.preventExtensions(obj); // no new properties - Define a property with metadata:
javascript Object.defineProperty(obj, "id", { value: 1, writable: false, enumerable: true, configurable: false });
Tips
- Use
Object.fromEntriesto rebuild an object from an array of pairs. Object.freezeis shallow; nested objects remain mutable unless recursively frozen.
Common issues
Object.assignmutates the first argument; pass an empty object to create a new one.- Property descriptors default to
falseforwritable,enumerable, andconfigurable.