Private setter typescript?

前端 未结 2 960
盖世英雄少女心
盖世英雄少女心 2020-12-30 18:32

Is there a way to have a private setter for a property in TypeScript?

class Test
{
    private _prop: string;
    public get prop() : string
    {
        re         


        
2条回答
  •  旧时难觅i
    2020-12-30 18:40

    The TypeScript specification (8.4.3) says...

    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 =....

    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.

    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';
        }
    }
    

提交回复
热议问题