我有一个 TypeScript 类定义,它是这样开始的;
module Entities {
export class Person {
private _name: string;
private _possessions: Thing[];
private _mostPrecious: Thing;
constructor (name: string) {
this._name = name;
this._possessions = new Thing[100];
}
看起来一个 Thing 类型的数组没有被正确地转换成相应的 Javascript 数组类型。下面是生成的 JavaScript 的一个片段:
function Person(name) {
this._name = name;
this._possessions = new Entities.Thing[100]();
}
执行包含 Person 对象的代码时,在尝试初始化 _ holdings 字段时引发异常:
错误是“0x800a138f-Microsoft JScript 运行时错误: 无法获取属性‘100’的值: 对象为 null 或未定义”。
如果我将 _ holdings 的类型更改为 any[]
,并且使用 new Array()
异常初始化 _ holdings,则不会引发异常。我错过什么了吗?