# Clone Objects And References

## Basic Concepts

OSL provides two ways to assign arrays and objects:

* `=` creates a deep clone (complete copy)
* `@=` creates a reference (points to the same data)

## Deep Cloning

When using `=`, OSL creates a complete copy of the object or array:

```javascript
// 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 4
```

## References

Using `@=` creates a reference to the original data:

```javascript
// Objects
original = {x: 1, y: {z: 2}}
reference @= original    // Creates a reference

reference.x = 10        // Affects both objects
reference.y.z = 20      // Affects both objects

log original.x          // Now 10
log original.y.z        // Now 20

// Arrays
arr1 = [1, 2, [3, 4]]
arr2 @= arr1           // Creates a reference

arr2[1] = 10           // Affects both arrays
arr2[3][1] = 40       // Affects both arrays

log arr1[1]            // Now 10
log arr1[3][1]         // Now 40
```

## When to Use Each

* Use `=` when you need a separate copy that can be modified independently
* Use `@=` when you want multiple variables to point to the same data
* Use `=` to prevent accidental modifications to the original
* Use `@=` to save memory when working with large data structures

## Important Notes

* `=` creates a complete copy of all nested structures
* `@=` maintains a single copy with multiple references
* Modifying a reference affects all variables pointing to the same data
* References are useful for passing large objects efficiently
* Deep clones are safer but use more memory


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://osl.mistium.com/arrays-and-objects/cloning-and-references.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
