Cloning an instance
const { attributes } = require('structure');
const User = attributes({
name: String,
})(class User {});
const user = new User({
name: 'Me',
});
const cloneUserWithNoOverwrite = user.clone(); // User { name: 'Me }
const cloneWithOverwrite = user.clone({ name: 'Myself' }); // User { name: 'Myself' }const { attributes } = require('structure');
const Book = attributes({
name: String,
})(class Book {});
const User = attributes({
name: String,
favoriteBook: Book,
})(class User {});
const user = new User({
name: 'Me',
favoriteBook: new Book({ name: 'The Silmarillion' }),
});
const cloneUserWithNoOverwrite = user.clone();
cloneUserWithNoOverwrite.favoriteBook === user.favoriteBook; // true, it was not cloned
const cloneWithOverwrite = user.clone({
favoriteBook: { name: 'The Lord of the Rings' },
});
cloneWithOverwrite.favoriteBook === user.favoriteBook; // false, it was **replaced** with the new value
cloneWithOverwrite.favoriteBook; // Book { name: 'The Lord of the Rings' }Strict mode
Last updated