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

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Baseline

20233

try...catch 1 C++ "friend"

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

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

js
class ClassWithSIB {
  static {
    // 
  }
}

js
class MyClass {
  static init() {
    // 
  }
}

MyClass.init();

init()

class static {}

var, function, const, let var

js
var y = "Outer y";

class A {
  static field = "Inner y";
  static {
    // var y 
    console.log(y); // undefined <-- not 'Outer y'

    var y = this.field;
  }
}

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

this super. super() arguments

await yield

1

js
class MyClass {
  static field1 = console.log("static field1");
  static {
    console.log("static block1");
  }
  static field2 = console.log("static field2");
  static {
    console.log("static block2");
  }
}
// 'static field1'
// 'static block1'
// 'static field2'
// 'static block2'

this super

this

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

super.property static

js
class A {
  static field = "static field";
}

class B extends A {
  static {
    console.log(super.field);
  }
}
// 'static field'

v8.dev blog

js
let getDPrivateField;

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

console.log(getDPrivateField(new D("private"))); // 'private'

ECMAScript 2027 LanguageSpecification
# prod-ClassStaticBlock


Web Proxy Viewer  |  New URL  |  Original Page