JavaScript

Shallow vs deep copy (JS)

Në shqip: Kopja e cekët dhe e thellë në JS

ShpjegimiSQ

{...obj} ose [...arr] bëjnë kopje të cekët: niveli i parë është i ri, por objektet brenda janë ende të përbashkëta. Për kopje të plotë përdor structuredClone().

EnglishEN

{...obj} or [...arr] make a shallow copy: the first level is new, but nested objects are still shared. For a full copy use structuredClone().

Si ta mendosh

Si ta fotokopjosh kopertinën e dosjes, por fletët brenda mbeten origjinalet.

Lexoje në anglisht

Like photocopying a folder's cover while the sheets inside stay the originals.

Shembull kodijavascript

const nx = { emri: "Arta", notat: [5, 4] };
const kopja = { ...nx };
kopja.notat.push(3);      // ndryshon edhe nx.notat!
const plote = structuredClone(nx);

Terma të lidhur