I was trying to implement some kind of factory that create a class definition in the run time,I figure this work my code look like this
function JsClassFactory() {
this.X = class {
};
this.addMethod = (name, def) => {
let Y = class extends this.X {
constructor() {
super()
this[name] = def
}
}
this.X = Y
return this;
}
this.addAttribute = (name, def) => {
this.addMethod(name, def)
}
this.getClass = function () {
return this.X
};
}
(function () {
const cf = new JsClassFactory()
cf.addAttribute('age', 35)
cf.addAttribute('name', 'chehimi')
cf.addMethod('myName', function () {
console.log(' i am %s', this.name)
})
cf.addMethod('myAge', function () {
console.log('my age is', this.age)
})
let z = new (cf.getClass())
z.myName();
z.myAge()
cf.addAttribute('name', 'ali')
cf.addAttribute('age', 15)
let t = new (cf.getClass())
t.myName();
t.myAge()
})()
i am asking if there is a better a way to implement this feature?
or a better work arround,