keepup-ECMAScript
CtrlK
  • About
  • Proposal
    • using
    • Temporal
  • ES2025
    • Promise.try
    • Sync Iterator helpers
    • Import Attributes
    • JSON Modules
    • New Set Methods
    • RegExp Modifiers
    • Duplicate named capture groups
  • ES2024
    • Resizable and growable ArrayBuffers
    • RegExp v flag with set notation + properties of strings
    • Atomics.waitAsync
    • Well-Formed Unicode Strings
    • array grouping
    • Promise withResolvers
  • ES2023
    • Symbols as WeakMap key
    • Hashbang Grammar
    • Array.findLast, findLastIndex
    • 配列の非破壊操作
  • ES2022
    • Private class fields
    • Regexp match indices
    • Top-level await
    • Private fields in-in
    • Array.at
    • Object.hasOwn
    • Class static block
    • Error cause
GitBook提供
このページ内
  • Example
  • Link
GitHubで編集
  1. ES2022

Class static block

クラス構文のstaticなフィールド・メソッド・ブロックが使えるようになった。

従来では動的にクラスの変数を追加することで実現していたが、よりクラスらしい宣言ができるようになった。

// 従来
class LegacyWay {}
LegacyWay.value = "";

Example

class Foo {
  static url;
  static #prefix = "/api";
  static {
    this.url = `${this.#y}/users`;
  }
}
console.log(Foo.url); // "/api/users"

Link

LogoGitHub - tc39/proposal-class-static-block: ECMAScript class static initialization blocksGitHub
LogoJavaScript classes: static | Can I use... Support tables for HTML5, CSS3, etc
Logostatic - JavaScript | MDN
前へObject.hasOwn次へError cause

最終更新 2 年前