且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TypeScript类型化数组的用法

更新时间: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();
        }
    }
}