~文法メモ~ クラスの基本クラスの骨格ECMAScript6ではよく見られる「class」を使って、クラスを定義出来るようになりました。 class MyWindow { constructor(x, y, w, h) { this.title = "僕のウィンドウ", this.x = x this.y = y this.w = w this.h = h } show() { return `タイトル:${this.title}, x:${this.x}, y:${this.y}, w:${this.w}, h:${this.h}`; } } let win = new MyWindow(10, 20, 500, 300); hm.debuginfo(win.show()); 匿名クラス関数同様に、クラスも匿名クラスが利用可能です。 let MyWindow = class { constructor(x, y, w, h) { this.title = "僕のウィンドウ", this.x = x this.y = y this.w = w this.h = h } show() { return `タイトル:${this.title}, x:${this.x}, y:${this.y}, w:${this.w}, h:${this.h}`; } } let win = new MyWindow(10, 20, 500, 300); hm.debuginfo(win.show()); function GetMyWindowClassType() { let T = class { constructor(x, y, w, h) { this.title = "僕のウィンドウ", this.x = x this.y = y this.w = w this.h = h } show() { return `タイトル:${this.title}, x:${this.x}, y:${this.y}, w:${this.w}, h:${this.h}`; } } return T; } let MyClass = GetMyWindowClassType(); hm.debuginfo(new MyClass(10,20,500,300).show()); |