How to initialize a get array? I was no longer trying to do anything:

import Cell from './Cell'; export default class Cells { Get: Cell[]; constructor(x: number, y: number) { for (let i = 0; i < x; i++) { this.Get[i] = new Cell[x]; for (let j = 0; j < y; j++) { this.Get[i][j] = new Cell(x, y, this._createHtmlElement()); } } } _createHtmlElement(): HTMLElement { let t = document.createElement("div"); t.classList.add("cell"); return t; } } 

In this implementation, the error is:

 TypeError: __WEBPACK_IMPORTED_MODULE_0__Cell__.a[x] is not a constructor 

Apparently the Cell class should have a constructor without parameters. Of course, I would have done it, if only it helped, but there still cannot be more than one constructor? What kind of nonsense?

    1 answer 1

    You simply specified the type and did not initialize the array.

     Get: Cell[] = []; 

    If Cell is a class, then you have an error.

     this.Get[i] = new Cell(x); 

    In js, it does not make sense to inherit an array (although it is possible with crutches). Http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ is well described what and how here.

    It turns out that the second-level array does not work. Because Get is Cell [].

      this.Get[i][j] //ошибка или не совсем правильно. 

    It turns out you need something similar to:

     export default class Cells { private Get: Cell[][] = []; constructor(x: number, y: number) { for (let i = 0; i < x; i++) { this.Get[i] = []; for (let j = 0; j < y; j++) { this.Get[i][j] = new Cell(x, y, this._createHtmlElement()); } } } _createHtmlElement(): HTMLElement { let t = document.createElement("div"); t.classList.add("cell"); return t; } }