Created
February 18, 2023 20:39
-
-
Save prof3ssorSt3v3/4ba747cce55a405122c90199bfcc7f08 to your computer and use it in GitHub Desktop.
Code from video about Structured Clone method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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