I must be doing something wrong.
- Instantiate Person class as Bob with name 'Bob'
- Clone Bob as new var Alice
- Rename Alice with name 'Alice'
- Log names of Bob & Alice
I expect Bob's name will remain 'Bob', but it has updated to 'Alice', despite not updating Bob...?
class Person {
constructor(attr) {
this.attr = attr;
}
talk() {
console.log('My name is ' + this.attr.name);
}
}
function clone(obj) {
return Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);
}
var Bob = new Person({
name: 'Bob'
});
var Alice = clone(Bob);
Alice.attr.name = 'Alice';
Alice.talk();
Bob.talk();
Thanks in advance.