Episode 1 — Fundamentals / 1.24 — Object Methods
1.24 — Object Methods: Quick Revision
Compact cheat sheet. Print-friendly.
How to use this material (instructions)
- Skim before labs or interviews.
- Drill gaps — reopen
README.md->1.24.a...1.24.h. - Practice —
1.24-Exercise-Questions.md. - Polish answers —
1.24-Interview-Questions.md.
All 8 methods at a glance
| Method | Returns | Purpose |
|---|---|---|
Object.keys(obj) | string[] | Own enumerable property names |
Object.values(obj) | any[] | Own enumerable property values |
Object.entries(obj) | [string, any][] | Own enumerable [key, value] pairs |
Object.fromEntries(iter) | object | Build object from [key, value] pairs |
Object.assign(target, ...src) | target | Copy props into target (mutates) |
Object.freeze(obj) | obj | Make object immutable (shallow) |
Object.seal(obj) | obj | Fix shape, allow value changes (shallow) |
Object.is(a, b) | boolean | Same-value equality (fixes NaN, +/-0) |
Iteration trio
const obj = { name: "Alice", age: 30 };
Object.keys(obj); // ["name", "age"]
Object.values(obj); // ["Alice", 30]
Object.entries(obj); // [["name", "Alice"], ["age", 30]]
Common rules for all three:
- Own properties only (not inherited)
- Enumerable only
- String keys only (no Symbols)
- Order: integer keys sorted, then string keys by insertion order
entries/fromEntries pipeline
// Filter
Object.fromEntries(Object.entries(obj).filter(([k, v]) => v > 0));
// Transform values
Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, v * 2]));
// Pick keys
Object.fromEntries(Object.entries(obj).filter(([k]) => allowed.includes(k)));
Object.assign quick reference
// Merge (safe — empty target)
const merged = Object.assign({}, defaults, overrides);
// Clone with extras
const clone = Object.assign({}, original, { extra: true });
// Mutate existing
Object.assign(target, source); // target is modified and returned
Remember:
- Shallow copy (nested objects shared by reference)
- Later sources overwrite earlier (last wins)
- Copies Symbol keys (unlike entries)
- Triggers setters on target
assign vs spread
Object.assign(target, src) | { ...src } | |
|---|---|---|
| Mutates target? | Yes | No |
| Triggers setters? | Yes | No |
| Creates new object? | Only if target is {} | Always |
Immutability hierarchy
preventExtensions < seal < freeze
(weakest) (strongest)
| Operation | preventExtensions | seal | freeze |
|---|---|---|---|
| Add properties | No | No | No |
| Delete properties | Yes | No | No |
| Modify values | Yes | Yes | No |
| Reconfigure descriptors | Yes | No | No |
All three are SHALLOW. Nested objects unaffected.
freeze vs const
const | Object.freeze() | |
|---|---|---|
| Protects | Variable binding | Object contents |
| Prevents reassignment? | Yes | No |
| Prevents property mutation? | No | Yes |
Use both: const CONFIG = Object.freeze({ ... })
Object.is vs === vs ==
== | === | Object.is() | |
|---|---|---|---|
| Type coercion? | Yes | No | No |
NaN === NaN | false | false | true |
+0 === -0 | true | true | false |
Rule of thumb: Use === everywhere. Use Object.is for NaN-safe comparisons (React does this).
Checking object state
Object.isFrozen(obj); // true if frozen
Object.isSealed(obj); // true if sealed (or frozen)
Object.isExtensible(obj); // true if can add properties
Common patterns
Empty check
Object.keys(obj).length === 0
Property count
Object.keys(obj).length
Value existence
Object.values(obj).includes(searchValue)
Sum values
Object.values(obj).reduce((sum, v) => sum + v, 0)
Object to Map
new Map(Object.entries(obj))
Map to Object
Object.fromEntries(map)
Object to query string
Object.entries(obj).map(([k, v]) => `${k}=${v}`).join("&")
Deep freeze
function deepFreeze(obj) {
Object.freeze(obj);
Object.values(obj).forEach(v => {
if (v && typeof v === "object" && !Object.isFrozen(v)) deepFreeze(v);
});
return obj;
}
Shallow clone
{ ...obj } // spread (preferred)
Object.assign({}, obj) // assign
Deep clone
structuredClone(obj) // modern (recommended)
JSON.parse(JSON.stringify(obj)) // JSON-safe data only
What each method copies/skips
| Own | Enumerable only | String keys | Symbol keys | |
|---|---|---|---|---|
Object.keys() | Yes | Yes | Yes | No |
Object.values() | Yes | Yes | Yes | No |
Object.entries() | Yes | Yes | Yes | No |
Object.assign() | Yes | Yes | Yes | Yes |
{ ...obj } | Yes | Yes | Yes | Yes |
for...in | +inherited | Yes | Yes | No |
Master workflow
- Inspect —
Object.keys/values/entriesto examine an object - Transform —
entries->map/filter->fromEntriespipeline - Copy —
{ ...obj }for shallow;structuredClonefor deep - Merge —
{ ...defaults, ...overrides }orObject.assign({}, defaults, overrides) - Lock —
Object.sealfor fixed shape;Object.freezefor full immutability - Compare —
===normally;Object.isfor NaN-safe checks
End of 1.24 quick revision.