LoginSignup
8
7

More than 5 years have passed since last update.

TypeScriptで匿名クラス・内部クラス

Last updated at Posted at 2018-04-19

匿名クラス(Anonymous class)のオブジェクトは以下のように作成できる。

anon.ts
class Base {
    public static create() {
        return new class extends Base {
            constructor() {
                super("XXX");
            }
        }();
    }

    constructor(private value: string) {
    }
    public methodA() {
        console.log("A" + this.value);
    }
}

const v = Base.create();
v.methodA();

内部クラス(Inner class)は以下のように

inner.ts
class Base {
    public static Inner = class extends Base {
        public method() {
            return 1;
        }
    };
}
8
7
0

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
8
7