最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to merge class instances in TypeScript? - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Jul 28, 2024 at 11:13 Iulian Onofrei 9,75711 gold badges71 silver badges117 bronze badges asked Apr 20, 2018 at 8:48 Michael HilusMichael Hilus 1,8283 gold badges27 silver badges48 bronze badges 3
  • 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() and Object.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
Add a ment  | 

2 Answers 2

Reset to default 4

Spread 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
}
发布评论

评论列表(0)

  1. 暂无评论