-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.js
More file actions
18 lines (14 loc) · 658 Bytes
/
code.js
File metadata and controls
18 lines (14 loc) · 658 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Deep clone using structuredClone (modern browsers / Node 17+)
// structuredClone preserves Dates, RegExp, Maps, Sets, and doesn't run code.
function deepClone(obj) {
return structuredClone(obj);
}
// Simple fallback: JSON deep copy — works for plain objects (no Date/Map/Set/functions)
function deepCloneFallback(obj) {
return JSON.parse(JSON.stringify(obj));
}
// Usage
const original = { name: "A", meta: { joined: new Date(), tags: ["js"] } };
const copy = (typeof structuredClone === "function") ? deepClone(original) : deepCloneFallback(original);
copy.meta.tags.push("node");
console.log(original.meta.tags); // original unaffected: ["js"]