@奧斯卡的回答是正確的。
這是一個更詳細的代碼示例;
enum memberTypes
{
None,
Property,
Method
}
class memberInfo {
memberName: string;
memberType: memberTypes;
constructor(name: string, mtype: memberTypes)
{
this.memberName = name;
this.memberType = mtype;
}
toString() {
return JSON.stringify(this);
}
}
class propertyInfo extends memberInfo
{
constructor(name: string)
{
super(name, memberTypes.Property);
}
}
class methodInfo extends memberInfo
{
constructor(name: string)
{
super(name, memberTypes.Method);
}
}
var dict = new Object();
var objectArray = new Array<memberInfo>();
objectArray.push(new propertyInfo("Name"), new methodInfo("toString"));
objectArray.forEach((item) => {
if (item.memberType === memberTypes.Property)
{
if (item.memberName === "Name") {
dict[item.memberName] = 'SysName';
}
} else if (item.memberType === memberTypes.Method) {
if (item.memberName === "toString") {
dict[item.memberName] = function() { return JSON.stringify(this); };
}
}
console.log(item.toString());
})
console.log(dict);
TypeScript Playground Example
的問題不在於你不能設置像這樣的財產,問題是,你正在試圖建立的「不確定」的屬性。 this.dict沒有定義 –