typescript 私人二传手打字稿?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27825350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Private setter typescript?
提问by sheamus
Is there a way to have a private setter for a property in TypeScript?
有没有办法在 TypeScript 中为属性设置私有设置器?
class Test
{
private _prop: string;
public get prop() : string
{
return this._prop;
}
private set prop(val: string)
{
//can put breakpoints here
this._prop = val;
}
}
Compiler complains that visibility for getter and setter don't match. I know I can just set the backing field, but but then I can't set breakpoints when the value is set.
编译器抱怨 getter 和 setter 的可见性不匹配。我知道我可以只设置支持字段,但是当设置值时我无法设置断点。
I though about using an interface to hide the setter, but interfaces can only define a property, not whether it has a getter on setter.
我虽然想使用接口来隐藏 setter,但接口只能定义一个属性,而不是它是否在 setter 上有 getter。
Am I missing something here? There doesn't seem to be any reason to not allow private setters, the resulting JS doesn't enforce visibility anyway, and seems better that the current alternatives.
我在这里错过了什么吗?似乎没有任何理由不允许私有 setter,由此产生的 JS 无论如何都不会强制执行可见性,而且似乎比当前的替代方案更好。
Am I missing something? If not is there a good reason for no private setters?
我错过了什么吗?如果不是,没有私人二传手有充分的理由吗?
采纳答案by Fenton
The TypeScript specification (8.4.3) says...
TypeScript 规范 (8.4.3) 说...
Accessors for the same member name must specify the same accessibility
相同成员名称的访问器必须指定相同的可访问性
So you have to choose a suitable alternative. Here are two options for you:
所以你必须选择一个合适的替代品。这里有两个选项供您选择:
You can just not have a setter, which means only the Test
class is able to set the property. You can place a breakpoint on the line this._prop =...
.
您可以没有设置器,这意味着只有Test
类才能设置属性。您可以在该行上放置一个断点this._prop =...
。
class Test
{
private _prop: string;
public get prop() : string
{
return this._prop;
}
doSomething() {
this._prop = 'I can set it!';
}
}
var test = new Test();
test._prop = 'I cannot!';
Probably the ideal way to ensure private access results in something akin to a "notify property changed" pattern can be implemented is to have a pair of private get/set property accessors, and a separate public get property accessor.
可能确保私有访问导致类似于“通知属性更改”模式的理想方法可以实现是拥有一对私有 get/set 属性访问器和一个单独的公共 get 属性访问器。
You still need to be cautious about someone later adding a direct call to the backing field. You could get creative in that area to try and make it less likely.
您仍然需要对稍后将直接调用添加到支持字段的人保持谨慎。您可以在该领域发挥创意,尝试减少这种可能性。
class Test
{
private _nameBackingField: string;
private get _name() : string
{
return this._nameBackingField;
}
private set _name(val: string)
{
this._nameBackingField = val;
// other actions... notify the property has changed etc
}
public get name(): string {
return this._name;
}
doSomething() {
this._name += 'Additional Stuff';
}
}
回答by splintor
I also hope we could have public getter and private setter. Until we do, another way to handle this is to add additional private getter and setter:
我也希望我们可以有公共 getter 和私有 setter。在我们这样做之前,另一种处理方法是添加额外的私有 getter 和 setter:
class Test {
_prop: string;
public get prop(): string {
return this._prop;
}
private get internalProp(): string {
return this.prop;
}
private set internalProp(value: string) {
this._prop = value;
}
private addToProp(valueToAdd: string): void {
this.internalProp += valueToAdd;
}
}