Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created February 18, 2023 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/4ba747cce55a405122c90199bfcc7f08 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/4ba747cce55a405122c90199bfcc7f08 to your computer and use it in GitHub Desktop.
Code from video about Structured Clone method
// structuredClone( ) method
const log = console.log;
//objects with primitives
let obj1 = { prop1: 'abc', prop2: 123 };
//objects containing objects
let names = ['fred', 'velma', 'daphne', 'shaggy'];
let obj2 = { prop3: 'def', prop4: names };
//objects with non-serializable props
let obj3 = {
prop5: true,
prop6: function () {
console.log('6');
},
};
let num = 8;
let other = num; //actually copying
let pointer1 = obj1; //copy the ref
log('pointer1', pointer1 === obj1);
let spread1 = { ...obj1 };
log('spread1', obj1 === spread1);
let pointer2 = { ...obj2 };
pointer2.prop4 = [...names];
log('pointer2', pointer2 === obj2);
let copy1 = structuredClone(obj1);
let copy2 = structuredClone(obj2);
log(copy1 === obj1);
log(copy2 === obj2);
log(copy1);
log(copy2);
try {
let copy3 = structuredClone(obj3);
} catch (err) {
console.log(err.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment