Не могу напрямую из объекта вызвать массив по ключу

_checkLine() {
  let massiv = {
    'x': [this._x],
    'y': [this._y]
  }

  for (let i = 1; i <= this._before; i++) {
    if (massivCell[this._x][this._y - i] && massivCell[this._x][this._y - i] == this._klas) {
      massiv['x'].push(this._x);
      massiv['y'].push(this._y - i);    
    } else { 
      break; 
    }
  }    

  for (let i = 1; i <= this._before; i++) {
    if (massivCell[this._x][this._y + i] && massivCell[this._x][this._y + i] == this._klas) {   
      massiv['x'].push(this._x);
      massiv['y'].push(this._y + i);    
    } else { 
      break; 
    }
  }
  return massiv;    
}

run_check() {
  if (this._checkLine()['x'].length == this._before) {
    return this._checkLine();
  } else if (this._checkColumn()['x'].length == this._before) {
    return this._checkColumn()
  } else if (this._checkDiagonal_1()['x'].length == this._before) {
    return this._checkDiagonal_1()
  } else if (this._checkDiagonal_2()['x'].length == this._before) {
    return this._checkDiagonal_2()
  }
}

borderColor() {
  let masiv = this.run_check();
  let masX = [];
  let masY = [];
  for (let key in masiv) {
    if (key == 'x')
      masX = masiv[key];
    else
      masY = masiv[key];
    }
    console.log(masiv['x'])

    for (let i = 0; i < masX.length; i++) {
      this._DivX[masX[i]].children[masY[i]].classList.add('border');
    }
  }
}

Проблема начинается в методе BorderColor. Я хотел вывести массив по ключу, то есть MASIV['X'], но высвечивается ошибка

Cannot read property 'x' of undefined

Как вы видите дальше, мне пришлось сделать пару манипуляций, чтобы вывести массив. Что интересно, так это то, что FOR OF не работает.

Но я так и не понимаю почему выводится такая ошибка, ведь в методе run_check я спокойно могу указать ключ.


Ответы (1 шт):

Автор решения: Igor

Потому что выполнение в run_check не заходит ни в один if/else if, и this.run_check() возвращает undefined.

borderColor() {
    let masiv = this.run_check();
    console.log(masiv); // - ?
    ...
→ Ссылка