creational.prototype.prototype_factory.js
creational.prototype.prototype_factory.js
{
constructor(suite, streetAddress, city)
{
this.suite = suite;
this.streetAddress = streetAddress;
this.city = city;
}
toString()
{
return `Suite ${this.suite}, ` +
`${this.streetAddress}, ${this.city}`;
}
}
toString()
{
return `${this.name} works at ${this.address}`;
}
greet()
{
console.log(
`Hi, my name is ${this.name}, ` +
`I work 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;
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);
}
}
class EmployeeFactory
{
static _newEmployee(proto, name, suite)
{
let copy = EmployeeFactory.serializer.clone(proto);
copy.name = name;
copy.address.suite = suite;
return copy;
}
console.log(john.toString());
console.log(jane.toString());