在typescript中获取类的属性

时间:2017-01-31 14:02:05

标签: javascript typescript

我有以下课程:

export class Test {

        private _rowsCount: string;
        public get RowsCount(): string {
            return this._rowsCount;
        };
        public set RowsCount(value: string) {
            this._rowsCount = value;
        };

        private _rowsCount2: string;
        public get RowsCount2(): string {
            return this._rowsCount2;
        };
        public set RowsCount2(value: string) {
            this._rowsCount2 = value;
        };
    }

我需要迭代特定类中的属性,我尝试了以下内容:

Object.keys(this).forEach((key)=> {
    console.log(key);
});

但是这个问题只是在private字段上进行了迭代,我还尝试了下面的内容,我得到了所有methodsproperties

    for (var property in this) {
        if (this.hasOwnProperty(property)) {
            console.log(property);                
        }
    }

有没有人有解决方案?

谢谢!

2 个答案:

答案 0 :(得分:6)

如果你只需要获得吸气剂/孵化器,那么你需要做类似的事情:

class Base {
    public static getGetters(): string[] {
        return Object.keys(this.prototype).filter(name => {
            return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function"
        });
    }

    public static getSetters(): string[] {
        return Object.keys(this.prototype).filter(name => {
            return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function"
        });
    }
}

class Test extends Base {
   ...
}

Test.getGetters(); // work the same

code in playground

您可以将静态方法放在基类中,然后在扩展它时,子类也将具有这些静态方法:

class Base {
    public getGetters(): string[] {
        return Object.keys(this.constructor.prototype).filter(name => {
            return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["get"] === "function"
        });
    }

    public getSetters(): string[] {
        return Object.keys(this.constructor.prototype).filter(name => {
            return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["set"] === "function"
        });
    }
}

code in playground

如果您希望这些方法是实例方法,那么您可以这样做:

this.prototype

更改不是使用this.constructor.prototype而是使用let a = new Test(); a.getGetters(); // ["RowsCount", "RowsCount2"] 然后你只需:

class Base {
    public static getGetters(): string[] {
        return Reflect.ownKeys(this.prototype).filter(name => {
            return typeof Reflect.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function";
        }) as string[];
    }

    public static getSetters(): string[] {
        return Reflect.ownKeys(this.prototype).filter(name => {
            return typeof Reflect.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function";
        }) as string[];
    }
}

code in playground

修改

基于@Twois的评论,他指出在定位es6时它不会起作用,这里有一个可行的版本:

Reflect.ownKeys(this.prototype)

主要区别:使用Object.keys(this.prototype)代替{ "require": { "twbs/bootstrap": "3.3.5" ... }, "scripts": { "your-cmd": [ "mkdir -p _libraries", "cp -r vendor/twbs/bootstrap/dist _libraries/bootstrap/", ... ], "post-install-cmd": [ "@your-cmd", ... ], "post-update-cmd": [ "@your-cmd", ... ] } }

答案 1 :(得分:1)

你可以做的是你想要使用它的类扩展上面的类并因此而公开属性;

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

在你的Test类中,使私有受保护:

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

然后你应该能够遍历外部类中的属性;

但是如果你想拥有这些价值观;为什么不通过在数组中返回值或将它们记录为字符串来创建一个公开值的函数;