Somthing in this direction? Here jobs is undefined in the functions addJob and runNext is there the possibility to get this work?
const myThing = {
jobs: [],
addJob: (id, job) => {
jobs.push({id: id, job: job})
},
runNext: () => {
jobs.shift().job()
}
}
CodePudding user response:
You can use a factory-function or a class to achieve this:
Factory Function:
function myThing() {
const jobs = [];
return {
addJob: (id, job) => {
jobs.push({
id: id,
job: job
})
},
runNext: () => {
jobs.shift().job()
},
print: () => {
console.log(jobs);
}
};
}
const instance = myThing();
instance.addJob("someId", "someJob")
instance.print();
Class:
class MyThing {
jobs = [];
addJob(id, job) {
this.jobs.push({
id: id,
job: job
});
}
runNext() {
this.jobs.shift().job()
}
print() {
console.log(this.jobs);
}
}
const instance = new MyThing();
instance.addJob("someId", "someJob");
instance.print();