Structure adds a method #clone in order to be able to create a shallow copy of an instance. This methods accepts an optional overwrite object that permits you to overwrite some attributes of the copy.
If the structure has a nested structure inside of it, the #clone method will not clone it but just point the new instance to the old value of the nested attribute.
const { attributes } =require('structure');constBook=attributes({ name: String,})(classBook {});constUser=attributes({ name: String, favoriteBook: Book,})(classUser {});constuser=newUser({ name:'Me', favoriteBook:newBook({ name:'The Silmarillion' }),});constcloneUserWithNoOverwrite=user.clone();cloneUserWithNoOverwrite.favoriteBook ===user.favoriteBook; // true, it was not clonedconstcloneWithOverwrite=user.clone({ favoriteBook: { name:'The Lord of the Rings' },});cloneWithOverwrite.favoriteBook ===user.favoriteBook; // false, it was **replaced** with the new valuecloneWithOverwrite.favoriteBook; // Book { name: 'The Lord of the Rings' }
Strict mode
When cloning an instance, you can clone it in strict mode as well, so if the resulting clone is invalid it throws an error. To do that, pass a second argument to the #clone method with the option strict as true.