'ReferenceError: Cannot access 'Block' before initialization

I'm writing some js code for doing tetris for a school project, and I got stuck with a class I wrote. This is the code:

let canvas = document.getElementById("mainWindow");
let cnv = canvas.getContext("2d");

class Block { //da fixare
  constructor(shape, color, rotate) {
    this.shape = shape;
    this.color = color;
    this.rotate = rotate;
  }
}

function randomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomColor(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  let color = Math.floor(Math.random() * (max - min + 1)) + min;
  switch (color) {
    case 1:
      return "red";
    case 2:
      return "yellow";
    case 3:
      return "blue";
    case 4:
      return "green"
    case 5:
      return "light-blue";
    case 6:
      return "indigo";
    case 7:
      return "orange";
  }
}

function newBlock() {
  let blockObj = new Block(randomInt(1, 1), randomColor(1, 1), randomInt(1, 1));
  console.log("figura generata: ");
  if (blockObj.shape == 1) {
    console.log("quadrato ");
  }
  if (blockObj.color == "red") {
    console.log("rosso ");
  }
  if (blockObj.rotate == 1) {
    console.log("orizzontale");
  }
}

newBlock();
<canvas id="mainWindow"></canvas>
When I try to call the newBlock() function, the console gives me this error:
Uncaught ReferenceError: Cannot access 'Block' before initialization
    at newBlock (tetris.js:43:5)
    at <anonymous>:1:1

I also searched on the internet for the solution but everyone says you have to declare the class before the function, as in my code. How can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source