Clone Objects And References
Basic Concepts
Deep Cloning
// Objects
original = {x: 1, y: {z: 2}}
clone = original // Creates a deep copy
clone.x = 10 // Only affects clone
clone.y.z = 20 // Only affects clone
log original.x // Still 1
log original.y.z // Still 2
// Arrays
arr1 = [1, 2, [3, 4]]
arr2 = arr1 // Creates a deep copy
arr2[1] = 10 // Only affects arr2
arr2[3][1] = 40 // Only affects arr2
log arr1[1] // Still 1
log arr1[3][1] // Still 4References
When to Use Each
Important Notes
Last updated