typescript 我想列出类/接口的所有公共属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36105098/
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
I want to list all public properties of a Class/Interface
提问by Khanh Hua
Using TypeScript we can define classes and their public properties. How can I get a list of all public properties defined for a class.
使用 TypeScript,我们可以定义类及其公共属性。如何获取为类定义的所有公共属性的列表。
class Car {
model: string;
}
let car:Car = new Car();
Object.keys(car) === [];
Is there a way to make Car emit its model
property?
有没有办法让 Car 发出它的model
属性?
回答by Moritz
Like Aaron already mentioned in the comment above, public and private members look the same in Javascript, so there cannot be a method that distinguishes between them. For instance, the following TypeScript Code
就像 Aaron 在上面的评论中已经提到的那样,公共和私有成员在 Javascript 中看起来是一样的,因此无法区分它们的方法。例如,下面的 TypeScript 代码
class Car {
public model: string;
private brand: string;
public constructor(model:string , brand: string){
this.model = model;
this.brand = brand;
}
};
is compiled to:
编译为:
var Car = (function () {
function Car(model, brand) {
this.model = model;
this.brand = brand;
}
return Car;
}());
;
As you can see, in the compiled JavaScript Version, there is absolutely no difference between the members model
and brand
, event though one of them is private and the other one is public.
如您所见,在编译后的 JavaScript 版本中,成员model
和brand
, event之间绝对没有区别,尽管其中一个是私有的,另一个是公共的。
You could distinguish between private and public members by using some naming convention (For instance, public_member
and __private_member
).
您可以通过使用一些命名约定(例如,public_member
和__private_member
)来区分私有成员和公共成员。
回答by John Manko
Updated answer (also see Crane Weirdo's answer on final JS public/private, which my answer doesn't address):
更新的答案(另请参阅 Crane Weirdo 对最终 JS 公共/私有的回答,我的回答没有解决):
class Vehicle {
axelcount: number;
doorcount: number;
constructor(axelcount: number, doorcount: number) {
this.axelcount = axelcount;
this.doorcount = doorcount;
}
getDoorCount(): number {
return this.doorcount;
}
}
class Trunk extends Vehicle {
make: string;
model: string;
constructor() {
super(6, 4);
this.make = undefined; // forces property to have a value
}
getMakeAndModel(): string {
return "";
}
}
Usage:
用法:
let car:Trunk = new Trunk();
car.model = "F-150";
for (let key in car) {
if (car.hasOwnProperty(key) && typeof key !== 'function') {
console.log(key + " is a public property.");
} else {
console.log(key + " is not a public property.");
}
}
Output:
输出:
axelcount is a public property.
doorcount is a public property.
make is a public property.
model is a public property.
constructor is not a public property.
getMakeAndModel is not a public property.
getDoorCount is not a public property.
Previous answer:
上一个答案:
class Car {
model: string;
}
let car:Car = new Car();
for (let key in car) {
// only get properties for this class that are not functions
if (car.hasOwnProperty(key) && typeof key !== 'function') {
// do something
}
}