更新时间:2022-11-22 20:02:30
您的语法有误:
this._possessions = new Thing[100]();
这不会创建事物数组".要创建事物数组,您可以简单地使用数组文字表达式:
This doesn't create an "array of things". To create an array of things, you can simply use the array literal expression:
this._possessions = [];
如果要设置长度,请使用数组构造函数:
Of the array constructor if you want to set the length:
this._possessions = new Array(100);
I have created a brief working example you can try in the playground.
module Entities {
class Thing {
}
export class Person {
private _name: string;
private _possessions: Thing[];
private _mostPrecious: Thing;
constructor (name: string) {
this._name = name;
this._possessions = [];
this._possessions.push(new Thing())
this._possessions[100] = new Thing();
}
}
}