0% found this document useful (0 votes)
4 views2 pages

creational.prototype.copy_through_serialization.js

The document defines three classes: Address, Person, and Serializer, which facilitate the creation and manipulation of address and person objects. The Serializer class includes methods for marking objects with type indices, reconstructing them, and cloning objects while preserving their structure. An example demonstrates creating a Person object, cloning it, and modifying the clone without affecting the original.

Uploaded by

qsp41768
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

creational.prototype.copy_through_serialization.js

The document defines three classes: Address, Person, and Serializer, which facilitate the creation and manipulation of address and person objects. The Serializer class includes methods for marking objects with type indices, reconstructing them, and cloning objects while preserving their structure. An example demonstrates creating a Person object, cloning it, and modifying the clone without affecting the original.

Uploaded by

qsp41768
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class Address

{
constructor(streetAddress, city, country) {
this.streetAddress = streetAddress;
this.city = city;
this.country = country;
}

toString()
{
return `Address: ${this.streetAddress}, ` +
`${this.city}, ${this.country}`;
}
}

class Person
{
constructor(name, address)
{
this.name = name;
this.address = address; //!
}

toString()
{
return `${this.name} lives at ${this.address}`;
}

greet()
{
console.log(
`Hi, my name is ${this.name}, ` +
`I live at ${this.address.toString()}`
);
}
}

class Serializer
{
constructor(types){
this.types = types;
}

markRecursive(object)
{
// anoint each object with a type index
let idx = this.types.findIndex(t => {
return t.name === object.constructor.name;
});
if (idx !== -1)
{
object['typeIndex'] = idx;

for (let key in object)


{
if (object.hasOwnProperty(key) && object[key] != null)
this.markRecursive(object[key]);
}
}
}

reconstructRecursive(object)
{
if (object.hasOwnProperty('typeIndex'))
{
let type = this.types[object.typeIndex];
let obj = new type();
for (let key in object)
{
if (object.hasOwnProperty(key) && object[key] != null) {
obj[key] = this.reconstructRecursive(object[key]);
}
}
delete obj.typeIndex;
return obj;
}
return object;
}

clone(object)
{
this.markRecursive(object);
let copy = JSON.parse(JSON.stringify(object));
return this.reconstructRecursive(copy);
}
}

let john = new Person('John',


new Address('123 London Road', 'London', 'UK'));

let jane = JSON.parse(JSON.stringify(john));

jane.name = 'Jane';
jane.address.streetAddress = '321 Angel St';

john.greet();
// this won't work
// jane.greet();

// try a dedicated serializer


let s = new Serializer([Person,Address]); // pain point
jane = s.clone(john);

jane.name = 'Jane';
jane.address.streetAddress = '321 Angel St';

console.log(john.toString());
console.log(jane.toString());

You might also like