gpt4 book ai didi

JavaScript/ typescript : Difference between `Object.assign({}, myClass)` /`Object.create(myClass)` and constructor `new MyClass()`

转载 作者:行者123 更新时间:2023-12-04 17:13:33 25 4
gpt4 key购买 nike

我希望我可以针对这个问题逐字发布我的项目,但我不能。
基本上,我有以下类(class):

class Lowest {
someValue: string

constructor(someValue: string) {
this.someValue = someValue
}
}
class Middle {
lowest: Lowest

constructor(someValue: string) {
this.lowest = new Lowest(someValue)
}
}
class Highest {
private _middle: Middle
otherValue: SomeOtherClass

// normal getter
public get middle(): Middle {
return this._middle
}

public set middle(next: Middle) {
this._middle = next
// notice: when `_middle` is set, I update another property!
otherValue.doSomething(this._middle)
}

constructor(config: { someValue: string }) {
this.middle = new Middle(config.somevalue)
}
}
在我的程序的某些地方,我引用了 Highest实例并需要更改其 middle.lowest.someValue子属性。现在,由于我无法在这里真正描述的架构原因,我需要更新 Highest.otherValue属性(property)每当 Highest.middle .当我使用 TypeScript 时,我只是在 Highest.middle 的 setter 中执行此操作.因此,我不能直接设置 Highest.middle.lowest达到某种值(value)。
我的第一种方法是:
const nextMiddle = Object.assign({}, highestInstance.middle)
nextMiddle.lowest.someValue = "some other thing"
highestInstance.middle = nextMiddle
然而,这最终导致了一些非常奇怪的问题。现在,我对执行 nextMiddle 的深度克隆没有真正的技术需求。 ,所以我用下面的代码克服了它:
const nextMiddle = highestInstance.middle
nextMiddle.lowest.someValue = "some other thing"
highestInstance.middle = nextMiddle
当我尝试最佳修复时,我实现了 Middle.copy()基本上只调用 new Middle() 的方法和 new Lowest()使用前一个实例的值。这也解决了我的技术问题,但让我更加困惑。
我知道简单地重新分配 highestInstance.middle 之间存在很大差异。并使用 Object.assign()克隆它,但我不明白为什么 Object.assign() 之间似乎没有区别和 new Middle()这三种克隆/重新分配方法的真正区别是什么?

最佳答案

had no real technical need for performing a deep clone of nextMiddle,so I overcame it with the following code:

Object.assign({}, highestInstance.middle)正在创建浅拷贝而不是深拷贝。
这里的问题是使用 Middle Setter是逼自己去执行 otherValue: SomeOtherClass仅当 middle已更新。
即使您只是这样做,它也会更新它:
high.middle.lowest.someValue = 'new value'
high.middle = high.middle; // setter triggered
一个可能的解决方案是创建一个回调链而不是使用 setter:
    type voidFun = () => void;

class Lowest {
private _someValue: string

public get someValue(): string {
return this._someValue
}

private update?: voidFun; // create an optional callback

public set someValue(next: string) {
this._someValue = next
this.update?.call(undefined);

}

constructor(someValue: string, fun?: voidFun) {
this._someValue = someValue
this.update = fun;// pass down
}
}

class Middle {
lowest: Lowest;

constructor(someValue: string, fun?: voidFun) {
this.lowest = new Lowest(someValue, fun)
}
}

class Highest {
private _middle: Middle
otherValue: any = {};


// normal getter
public get middle(): Middle {
return this._middle
}

// now you dont need middle setting so it cant be updated by anyone directly
// if you still want to have you can have it too
private callBack = () => {
console.log('lower was update');
this.otherValue.myMiddle = this._middle.lowest.someValue;
}

constructor(config: { someValue: string } = { someValue : 'constr' }) {
this._middle = new Middle(config.someValue, this.callBack)
}
}



let high = new Highest()
high.middle.lowest.someValue = 'new value'
// high.middle = high.middle; // now not possible unless you create setter
console.log('updated middle high',high)

关于JavaScript/ typescript : Difference between `Object.assign({}, myClass)` /`Object.create(myClass)` and constructor `new MyClass()` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69051848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com