1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

変数・クラスの初期化前の参照(JavaScript)

Last updated at Posted at 2024-11-05

こんにちは。
JavaScript では変数・クラスの初期化前の参照は許されません。

下記を実行させると、エラーとみなされますので(Cannot access ... before initialization)、class 文を前置する必要があります。ただし bun run ./color.js ではエラーは生じません。

なお、function 文は後置記述が許されます。

color.js
const color = generate_color();
console.log(color);

class Color{
  constructor() {
    this.R = getRandomInt(255);
    this.G = getRandomInt(255);
    this.B = getRandomInt(255);
  }
}

function generate_color() {
  return new Color();
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?