Skip to content

Instantly share code, notes, and snippets.

View StephenChips's full-sized avatar

Dongcai Huang StephenChips

View GitHub Profile
@StephenChips
StephenChips / deepClone.js
Last active April 12, 2022 09:35
Deep-clone JavaScript objects.
/**
* @author StephenChips
*
* Notes:
* 1. There is no need to have a stack if the function is recursive.
* 2. To ensure every object only has one copy, you have to use a Map to map the
* objects to their copies.
* 3. Unlike toJSON, function deepClone should be able to handle cyclic objects.
* 4. Some object cannot be clone (or doesn't have a relaible method) and should just
* return themselves directly.