creational.prototype.explicit_copying.js
creational.prototype.explicit_copying.js
{
constructor(streetAddress, city, country) {
this.streetAddress = streetAddress;
this.city = city;
this.country = country;
}
deepCopy()
{
return new Address(
this.streetAddress,
this.city,
this.country
);
}
toString()
{
return `Address: ${this.streetAddress}, ` +
`${this.city}, ${this.country}`;
}
}
class Person
{
constructor(name, address)
{
this.name = name;
this.address = address; //!
}
deepCopy()
{
return new Person(
this.name,
this.address.deepCopy() // needs to be recursive
);
}
toString()
{
return `${this.name} lives at ${this.address}`;
}
}
jane.name = 'Jane';
jane.address.streetAddress = '321 Angel St'; // oops