From 93ec461b09c2636bbd3a51abdad8bc9f6cfd6802 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Mon, 6 Mar 2023 12:04:46 +0100 Subject: [PATCH] Move initialization logic for sub-classes to `Class` constructor (#8872) --- src/core/Class.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/core/Class.js b/src/core/Class.js index 1beaa03ae..ca580ccad 100644 --- a/src/core/Class.js +++ b/src/core/Class.js @@ -13,21 +13,7 @@ export class Class { // [Extends the current class](#class-inheritance) given the properties to be included. // Returns a Javascript function that is a class constructor (to be called with `new`). static extend({statics, includes, ...props}) { - const NewClass = function (...args) { - - Util.setOptions(this); - - // call the constructor - if (this.initialize) { - this.initialize.apply(this, args); - } - - // call all constructor hooks - this.callInitHooks(); - }; - - // inherit parent's prototype - Object.setPrototypeOf(NewClass.prototype, this.prototype); + const NewClass = class extends this {}; // inherit parent's static properties Object.setPrototypeOf(NewClass, this); @@ -90,6 +76,20 @@ export class Class { return this; } + _initHooksCalled = false; + + constructor(...args) { + Util.setOptions(this); + + // call the constructor + if (this.initialize) { + this.initialize(...args); + } + + // call all constructor hooks + this.callInitHooks(); + } + callInitHooks() { if (this._initHooksCalled) { return;