I am a newbie to nodejs. I am writing a helper function to build JSON using a schema, I am trying to add functions (mostly setters) to set values. The following is a simple version of it.
function Task() {
this.action = {};
this.schedule = {};
}
function capitalize(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
const scheduleProps = [
'startAt',
'repeatEvery',
'endAt',
'count',
'interval'
];
Add methods to it dynamically
for(var i=0; i<scheduleProps.length; i++) {
Object.defineProperty(Task.prototype, `set${capitalize(scheduleProps[i])}`, {
enumerable: true,
configurable: false,
writable: true,
value: (value) => {
this.schedule[scheduleProps[i]] = value;
}
});
}
When I call the following way I expect obj.schedule.repeatEvery to contain value 10.
obj = new Task();
obj.setRepeatEvery(10);
Instead I get
TypeError: Cannot set property 'repeatEvery' of undefined
I even tried to set the functions like so
Task.prototype[`set${capitalize(scheduleProps[i])}`] = (val) => {
this.schedule[scheduleProps[i]] = val;
}
In this case I get
TypeError: Cannot set property 'interval' of undefined
at Task.(anonymous function) [as setRepeatEvery]
How can I set methods to a function.prototype dynamically? Thanks a lot for your help
thisscope if that loop doesn't run inside a member method. try using a regularfunctionObject.defineProperty()prefered overTask.prototype[methodName]?forloop has already run to completion so yourivariable is wrong when your value method gets called.