المسارات / JavaScript / المرجع / structuredClone

مرجع · Object

structuredClone

structuredClone نسخة عميقة مستقلة من القيمة: objects متداخلة وarrays وMap وSet وDate، كلها بتتنسخ بالكامل.

What does structuredClone() do in JavaScript?

js
structuredClone(value)

النسخة مستقلة تمامًا: التعديل فيها مابيأثرش في الأصل ولا العكس. بتتعامل مع المراجع الدائرية من غير error. مابتنسخش الدوال ولا عناصر الـDOM — بترمي DataCloneError.

البرهان

شُغّل على Node v26.14.0، والناتج منسوخ من التشغيل نفسه.

js
const order = { id: 1, items: [{ name: 'كتاب', qty: 2 }], at: new Date(0) };
const copy = structuredClone(order);
copy.items[0].qty = 5;
console.log(order.items[0].qty, copy.items[0].qty);
console.log(copy.at instanceof Date, copy.items !== order.items);
const c = {};
c.me = c;
const cc = structuredClone(c);
console.log(cc.me === cc, cc !== c);
try {
  structuredClone(() => 1);
} catch (e) {
  console.log(e.constructor.name);
}
النتيجة
2 5
true true
true true
DOMException

الدروس اللي بتشرحها

الصفحة دي على MDN ↗

ابعت الصفحة لحد
واتسابتليجرامX
برهان

الدروس

اتحلّ 0 من 174 تمرين