Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How to create Factory Functions instead of using Classes
Post
How to create Factory Functions instead of using Classes
+1
−1
Instead of classes, I want to use factory functions.
Original code with classes:
export class MyProject {
constructor(title, description, dueDate, priority) {
this.title = title;
this.description = description;
this.dueDate = dueDate;
this.priority = priority;
this.todos = [];
this.projectIndex = null;
this.expandContent = () => expandContent(this);
this.deleteCard = () => deleteCard(this);
this.addTodo = (todoText) => {
this.todos.push(todoText);
this.expandContent();
};
}
}
I tried using factory functions:
export const MyProject = (title, description, dueDate, priority) => {
let todos = [];
let projectIndex = null;
const expandContent = () => expandContent(this);
const deleteCard = () => deleteCard(this);
const addTodo = (todoText) => {
todos.push(todoText);
expandContent();
}
return {title, description, dueDate, priority, todos, projectIndex, expandContent, deleteCard, addTodo}
};
I've acknowledged, that this
is not necessary in factories, I'm not really sure how not to use them in expandContent and deleteCard functions
2 comment threads