Untitled

 avatar
unknown
javascript
3 years ago
1.1 kB
4
Indexable
const data = {
    name: 'ash',
    grades: {
        math: 50,
        science: 200,
        english: 10,
    }
}


// Clone with reference
const dataClone1 = data;
dataClone1.name= 'ash2';
dataClone1.grades.math =0;
console.log(data);
console.log(dataClone1);


//Removes reference of level 1 objects
const dataClone2 = {...data};
dataClone2.name= 'ash3';
dataClone2.grades.math =0;
console.log(data);
console.log(dataClone2);

//Removes reference of level 1 objects 
const dataClone3 = Object.assign({}, data);
dataClone3.name= 'ash4';
dataClone3.grades.math =0;
console.log(data);
console.log(dataClone3);

//No reference 
const dataClone4 = JSON.parse(JSON.stringify(data));
dataClone4.name= 'ash5';
dataClone4.grades.math =0;
console.log(data);
console.log(dataClone4);

//No reference, Node 17+
const dataClone5 = structuredClone(data);
dataClone5.name= 'ash5';
dataClone5.grades.math =0;
console.log(data);
console.log(dataClone5);


data.func = func1 = () =>  10;

const dataClone6 = JSON.parse(JSON.stringify(data)); // No error omits function
const dataClone7 = structuredClone(data);// DOMException cannot clone func
Editor is loading...