I have a class like this:
class Person {
private _age: number;
get age(): number {
return this._age;
}
set age(value: number) {
this._age = value;
}
}
And an instance of that class:
let peter = new Person();
peter.age = 30;
I want to have another instance of that class by simply using the spread operator:
let marc = {
...peter,
age: 20
}
But this does not result in an instance of Person
but in an object without the getter and setter of Person
.
Is it possible to merge class instances somehow or do I need to use new Person()
again for mark
?
I have a class like this:
class Person {
private _age: number;
get age(): number {
return this._age;
}
set age(value: number) {
this._age = value;
}
}
And an instance of that class:
let peter = new Person();
peter.age = 30;
I want to have another instance of that class by simply using the spread operator:
let marc = {
...peter,
age: 20
}
But this does not result in an instance of Person
but in an object without the getter and setter of Person
.
Is it possible to merge class instances somehow or do I need to use new Person()
again for mark
?
- You'll need to use new Person() again for marc to create a seperate instance of the class Person for marc. It doesn't really make sense either to spread peter, unless marc is peters clone. ;) – Shilly Commented Apr 20, 2018 at 8:51
- Thanks. Yeah actually the example is not the best. Indeed I want to have a clone of Peter and work with the clone without modifying Peter. – Michael Hilus Commented Apr 20, 2018 at 8:57
-
If you don't want to modify peter, why do you need access to its getters and setters?
Object.entries()
andObject.values()
and such will give you an array containing copies of those properties ( providing they are primitives, else they'll be references ) on peter that you can safely work with. But does not give you the getters and setters. So it goes back to just creating a new instance again if you do need the getters and setters, but want them to be tied to their own scope instead of peters. – Shilly Commented Apr 20, 2018 at 9:03
2 Answers
Reset to default 4Spread syntax is supposed to produce plain object, so it isn't applicable. If new class instance is needed, it should be created with new
. If an object has to be merged with other properties, Object.assign
can be used:
let marc = Object.assign(
new Person()
peter,
{ age: 20 }
);
Since Object.assign
processes own enumerable properties, the result may be undesirable or unexpected, depending on class internals; it will copy private _age
but not public age
from peter
.
In any special case a class should implement utility methods like clone
that is aware of class internals and creates an instance properly.
In OOP terms you don't define your properties after creating instances, you define them in your class
In your example both Marc and Peter have an age so you don't need to merge anything. You could use the contructor to pass a specific value for that instance:
let marc = new Person("marc", 20)
let peter = new Person("peter", 30)
class Person {
constructor(private name:string, private age:number){
console.log("I am " + this.name + " my age is " + this.age)
}
}
If there is a property that no person except Marc has, then you can let Marc extend Person.
class Person {
}
class Marc extends Person {
car:string
}