这是一个很难回答的问题,我知道很多程序都存在问题(我最后会详细说明)。 我想在typescript中创建一个自定义setter,但是要设置的属性的数据类型不只是一个数字,字符串,bool它实际上是一个类对象。这很好 - 但是如果修改了类实例的属性,则不会调用setter 。以下是这种情况的一个例子:
//This class contains two properties
class Point
{
public x : number;
public y : number;
constructor(x : number, y : number) { this.x = x; this.y = 0; }
}
//How here is another class that contains a Point
//But it is private and a getter/setter is used
class PointStore
{
private _foo : Point;
public get foo() : Point { return this._foo; }
//Here is the problem, the setter is only called when the whole of foo is changed
public set foo(n : Point) { this._foo = n; console.log("Foo has been set!"); }
constructor() { this._foo = new Point(0, 0); }
}
//Use case
let bar : PointStore = new PointStore();
bar.foo = new Point(10, 10); //Logs "Foo has been set!"
bar.foo.x = 20; //Doesn't log anything
问题非常清楚,但我只想说以下内容:
无论如何都有这个吗?因为我从Unity3D这样的API中看到过,他们选择让他们的'Point'类只有私有成员,所以数据只能通过构造函数来设置,例如:
//the 'Unity' solution
transform.position = new Vector2(10, 10); //Okay
transform.position.x = 20; //Error
但这对问题来说根本不是一个完美的解决方案,因为从那时起使用'Point'类编程变得更加困难。
如果有人有解决这个问题的技巧,我们将不胜感激。
答案 0 :(得分:2)
您可以使用a Proxy:
class PointStore {
private _foo: Point;
constructor() {
this.createProxy(new Point(0, 0));
}
public get foo(): Point { return this._foo; }
public set foo(point: Point) {
this.createProxy(point);
console.log("Foo has been set!");
}
private createProxy(point: Point) {
this._foo = new Proxy(point, {
set: function (target: Point, property: string, value: any) {
target[property] = value;
console.log("Foo has been set (using proxy)!");
return true;
}
});
}
}
答案 1 :(得分:0)
只有在为属性赋值时才会使用setter。您可以绕过这种方法的一种方法是使用Object.assign
,如下所示:
bar.foo = new Point(10, 10);
bar.foo = Object.assign(bar.foo, {x: 20})
你也可以更进一步:
bar.foo = Object.assign(bar.foo, {x: {z: 20} })
答案 2 :(得分:0)
关于打字稿的好处是,如果它们具有相同的形状,则两种类型是兼容的。所以,基于你所描述的内容,我认为这似乎符合要求,这不会破坏你的代码,因为PointStore
这里与Point
类
class PointStore
{
private x : number;
private y : number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
};
public get x() { return this.point.x; };
public set x(x: number ) {
// your custom logic here
this.point.x = x;
};
// setter and getter for other y omitted
}