0
我開始使用TypeScript進行一些操作。我創建了兩個類(Student和Listview)。我試圖用我創建的學生對象遍歷數組,但不知何故它不起作用。通過TypeScript中的自定義對象數組循環遍歷
class Student {
fullName: string;
constructor(public firstName, public middleInitial, public lastName) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
class Listview {
items: Array<Student>;
constructor(public item_list: Array<Student>) {}
log(): void {
var items = this.items;
for(var i=0; i<items.length; i++) {
console.log(items[i]);
}
}
}
var list = new Listview(
[new Student("Jane", "M.", "User"),
new Student("Hans", "M.", "Muster"),
new Student("Fritz", "B.", "Muster")]
);
list.log();
我在控制檯中這樣的警告:
如何做我需要訪問數組中讀取每個學生對象的屬性?
問候 Orkun
你不設置'items'在你的代碼。你只需在構造函數中初始化'item_list'。 –
@Orkun將代碼/控制檯輸出直接放入StackOverflow問題被認爲是一種很好的禮儀。我建議編輯它包括代碼:) – JKillian
@SebastianSebald謝謝。由於我愚蠢的錯誤,沒有看到它以某種方式,因爲我公開它使item_list包含我的對象:) –