Get properties of class in typescript

后端 未结 2 1726
有刺的猬
有刺的猬 2021-01-07 23:10

I\'ve the following class:

export class Test {

        private _rowsCount: string;
        public get RowsCount(): string {
            return this._rowsCou         


        
2条回答
  •  粉色の甜心
    2021-01-08 00:05

    What you can do is for the class you want to use it extend the class above and make the properties public for this reason;

       class TestExposed extend Test {
          public _rowsCount: string;
          public _rowsCount2: string; 
       }
    

    And in your Test class make the private protected:

    class Test {
        protected _rowsCount: string;
        public get RowsCount(): string {
            return this._rowsCount;
        };
        public set RowsCount(value: string) {
            this._rowsCount = value;
        };
    
        protected _rowsCount2: string;
        public get RowsCount2(): string {
            return this._rowsCount2;
        };
        public set RowsCount2(value: string) {
            this._rowsCount2 = value;
        };
    }
    

    Then you should be able to iterate over the properties in an external class;

    But if you want to have the values; Why not make a function that exposes the values by returning them in an array or log them as a string;

提交回复
热议问题