Docs: Explain shallow copy behavior for slice method #320
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
##The Problem:Question-5.
The current documentation for Array.prototype.slice() is correct but incomplete. It omits the crucial detail that it performs a shallow copy. This can be misleading and lead to hard-to-debug issues when developers work with arrays of objects, as the original array's nested objects can be unintentionally mutated.
The Solution
This PR addresses this gap by making the behavior explicit and providing a clear example. The changes include:
Adding a new subsection, "A Note on Shallow Copying", to the slice() documentation.
Clearly explaining that slice() copies references to nested objects, not the objects themselves.
Providing a concise code example that demonstrates this effect.
Updating the slice vs. splice comparison table to reflect this important detail for quick reference.
The Benefit
This clarification will help developers avoid common pitfalls and prevent unintentional mutations. It makes the documentation more robust and helpful, especially for those who are new to this concept.