What if changing one object secretly changes another? Discover how fresh object literals save you from this hidden trap!
Fresh object literals vs variable assignment behavior in Typescript - When to Use Which
Imagine you want to create multiple objects that look similar but have their own separate values. You try to copy one object by just assigning it to a new variable, thinking they will be independent.
But when you change one object, the other changes too! This happens because the variables point to the same object in memory. Manually tracking and copying each property is slow and easy to mess up.
Using fresh object literals means creating a brand new object each time. This way, each variable holds its own separate object, so changes don't affect others. It's clean, safe, and easy to understand.
const obj1 = { name: 'Alice' };
const obj2 = obj1;
obj2.name = 'Bob';
console.log(obj1.name); // Outputs 'Bob'const obj1 = { name: 'Alice' };
const obj2 = { ...obj1 };
obj2.name = 'Bob';
console.log(obj1.name); // Outputs 'Alice'It lets you create independent objects effortlessly, avoiding unexpected bugs and making your code more reliable.
Think of making two separate user profiles in an app. You want to change one profile's name without accidentally changing the other. Fresh object literals make this safe and simple.
Assigning variables copies references, not objects.
Fresh object literals create new, independent objects.
This prevents bugs from shared object changes.
Practice
let b = a;?Solution
Step 1: Understand variable assignment with objects
Assigning one object variable to another copies the reference, not the object itself.Step 2: Effect of reference copying
Both variables now point to the same object in memory, so changes via one affect the other.Final Answer:
Both variables point to the same object in memory. -> Option DQuick Check:
Object assignment copies reference = A [OK]
- Thinking a new object is created on assignment
- Assuming variables hold independent copies
- Confusing object assignment with primitive assignment
Solution
Step 1: Identify fresh object literal syntax
Using curly braces{}creates a new, fresh object literal.Step 2: Check other options
let obj = Object.assign({}); is incorrect becauseObject.assign({})needs a source object to copy from; let obj = obj2; copies reference; let obj = new Object(obj2); creates an object but with a reference or unexpected behavior.Final Answer:
let obj = { name: 'Alice' }; -> Option CQuick Check:
Fresh object literal uses {} = A [OK]
- Using assignment to copy objects instead of fresh literals
- Misusing Object.assign without source
- Confusing new Object() with fresh literal
let a = { x: 1 };
let b = a;
b.x = 5;
console.log(a.x);Solution
Step 1: Understand reference assignment
Variablebpoints to the same object asa.Step 2: Modify property via
Changingbb.xto 5 changes the shared object property.Step 3: Log
Sincea.xaandbreference the same object,a.xis now 5.Final Answer:
5 -> Option BQuick Check:
Shared reference means change visible = B [OK]
- Expecting original object to remain unchanged
- Confusing object copying with reference assignment
- Assuming primitive copy behavior for objects
let obj1 = { val: 10 };
let obj2 = obj1;
obj2 = { val: 20 };
console.log(obj1.val);Solution
Step 1: Analyze initial assignment
obj2 initially points to the same object as obj1.Step 2: Reassign obj2 to a new object
obj2 now points to a fresh object; obj1 still points to the original.Step 3: Check console output
obj1.val remains 10 because obj1's reference did not change.Final Answer:
obj2 assignment creates a new object, obj1 remains unchanged. -> Option AQuick Check:
Reassignment changes reference, original unaffected = D [OK]
- Thinking obj1 changes when obj2 is reassigned
- Confusing property change with variable reassignment
- Expecting both variables to point to new object
const obj1 = { a: 1 };
const obj2 = ???
obj2.a = 5;
console.log(obj1.a);Solution
Step 1: Understand shallow copy with spread operator
Using{ ...obj1 }creates a fresh object with copied properties.Step 2: Check other options
obj2 = obj1; copies reference; obj2 = Object.assign(obj1); is incorrect usage of Object.assign; obj2 = obj1 as any; is just a type assertion, no copy.Step 3: Confirm independence
Changingobj2.adoes not affectobj1.a.Final Answer:
obj2 = { ...obj1 }; -> Option AQuick Check:
Spread operator creates fresh object copy = C [OK]
- Assigning reference instead of copying
- Misusing Object.assign without target
- Confusing type assertion with copying
Ready to prove it?
Pass the test and this pattern moves unknown
