Attempting to instantiate a class within a static method, I am using Object.create(this.prototype)
, which appears to be functioning correctly. Nonetheless, when I check the console, my property items
is showing as undefined
.
The base class called model looks like this:
export default class model {
protected items: any = {}
public constructor(options: ModelSettings) {
// Implement some logic here
// items property not being set before compilation
}
public static create(options) {
let t = Object.create(this.prototype) as any
console.log(t.items)
}
}
Next, there is a class named purchases that extends the model:
export default class purchases extends model {
public constructor() {
super({table: 'purchases'})
}
}
To invoke it, use the following code snippet:
purchases.create({ my: 'options' })
The create method instantiates an object of purchases
and operates smoothly. However, the property items
remains undefined
, as mentioned previously.
Could it be that Object.create()
does not execute the constructor?