Move initialization logic for sub-classes to Class constructor (#8872)

This commit is contained in:
Jon Koops
2023-03-06 12:04:46 +01:00
committed by GitHub
parent bedad7e850
commit 93ec461b09

View File

@ -13,21 +13,7 @@ export class Class {
// [Extends the current class](#class-inheritance) given the properties to be included. // [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`). // Returns a Javascript function that is a class constructor (to be called with `new`).
static extend({statics, includes, ...props}) { static extend({statics, includes, ...props}) {
const NewClass = function (...args) { const NewClass = class extends this {};
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);
// inherit parent's static properties // inherit parent's static properties
Object.setPrototypeOf(NewClass, this); Object.setPrototypeOf(NewClass, this);
@ -90,6 +76,20 @@ export class Class {
return this; 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() { callInitHooks() {
if (this._initHooksCalled) { if (this._initHooksCalled) {
return; return;