gpt4 book ai didi

javascript - 在 typescript 中获取类的属性

转载 作者:可可西里 更新时间:2023-11-01 01:53:18 25 4
gpt4 key购买 nike

我有以下类(class):

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

有没有人有解决办法?

谢谢!

最佳答案

如果您只需要获取 getters/setters,那么您需要执行如下操作:

class Test {
...

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

Test.getGetters(); // ["RowsCount", "RowsCount2"]
Test.getSetters(); // ["RowsCount", "RowsCount2"]

( code in playground )


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

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

变化在于,您使用的不是 this.prototype,而是 this.constructor.prototype
然后你只需:

let a = new Test();
a.getGetters(); // ["RowsCount", "RowsCount2"]

( code in playground )


编辑

根据@Twois 的评论,他指出在以 es6 为目标时它不会工作,这里有一个可以工作的版本:

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

主要区别:使用 Reflect.ownKeys(this.prototype) 而不是 Object.keys(this.prototype)

关于javascript - 在 typescript 中获取类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com