[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks [Back]  [Original]

Class static initialization blocks - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Class static initialization blocks

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2023 3.

.

, try...catch .

. . (C++ "friend" )

In this article

class ClassWithStaticInitializationBlock {
  static staticProperty1 = "Property 1";
  static staticProperty2;
  static {
    this.staticProperty2 = "Property 2";
  }
}

console.log(ClassWithStaticInitializationBlock.staticProperty1);
// Expected output: "Property 1"
console.log(ClassWithStaticInitializationBlock.staticProperty2);
// Expected output: "Property 2"

js
static { /*  */ }

static {} . . super .

. static {} var, function, const let var .

js
var y = " y";

class A {
  static field = " y";
  static {
    var y = this.field;
  }
}

//    var  
console.log(y);
// > ' y'

this . super.<property> super . super() .

lexical scope .

. ( )

. .

js
class MyClass {
  static field1 = console.log("1 ");
  static {
    console.log("1    ");
  }
  static field2 = console.log("2 ");
  static {
    console.log("2    ");
  }
}

/*
> "1 "
> "1    "
> "2 "
> "2    "
*/

super .

this super

this . .

js
class A {
  static field = " ";
  static {
    console.log(this.field);
  }
}

super.property static super . .

js
class A {
  static fieldA = "A.fieldA";
}
class B extends A {
  static {
    console.log(super.fieldA);
    // 'A.fieldA'
  }
}

. (v8.dev blog ).

js
let getDPrivateField;

class D {
  #privateField;
  constructor(v) {
    this.#privateField = v;
  }
  static {
    getDPrivateField = (d) => d.#privateField;
  }
}

getDPrivateField(new D("private"));
// > private

2

ES13 .

.

Specification
ECMAScript 2027 LanguageSpecification
# prod-ClassStaticBlock


Web Proxy Viewer  |  New URL  |  Original Page