If I compose some methods with factory function below.
function createCrudMethods() {
return {
findMany: async () => {
},
findOne: async () => {
},
createOne: async () => {
},
deleteOne: async () => {
},
updateOne: async () => {
},
};
}
Can I do something like this?
const object = {
...createCrudMethods();
createOneWithNumber() {
// do some cool stuff
createOne() // error undefined
}
}
or maybe there is a better way to do this?
You can make use of function
scope to refer to yourself using this
:
function createCrudMethods() {
return {
findMany: () => console.log('findMany'),
findOne: () => console.log('findOne'),
createOne: () => console.log('createOne'),
deleteOne: () => console.log('deleteOne'),
updateOne: () => console.log('updateOne')
};
}
const object = {
...createCrudMethods(),
createOneWithNumber() {
// do some cool stuff
console.log('createOneWithNumber');
this.createOne();
}
};
// Console should show:
// - createOneWithNumber
// - createOne
object.createOneWithNumber();