So I've been looking into factory functions and classes, looking for the most optimized way of creating multiple objects with several functions/operations each.
Let's say I am doing a TODO list, every task I create has 7 parameters and even more operations but mainly getters and setters.
If I create each of these tasks using a factory function every object created will have almost a dozen functions in memory for each of the tasks created right?
Since we have Object.create()
to create an object as the prototype of another one, would it be better to separate taskOperations
as a standalone object and using Object.create(taskOperations)
like I did below to avoid clogging the memory with the same functions for all the objects created?
Would this be the most optimized way or is using classes for example more effective for this?
const taskOperations = {
getTitle() {
return this.title;
},
setTitle(title) {
this.title = title;
},
getDateCreated() {
return this.dateCreated;
},
setDateDue(dateDue) {
this.dateDue = dateDue;
},
getDateDue() {
return this.dateDue;
},
setPriority(priority) {
this.priority = priority;
},
getPriority() {
return this.priority;
},
};
// Task factory
const createTask = (title) => {
let task = Object.create(taskOperations);
task.title= title;
task.dateCreated= new Date();
task.dateDue= "";
task.priority= false;
task.note= "";
task.done= false;
task.projectID= 0;
return task
};