In the past, I would create classes with a type property in traditional OOP languages such as Swift or Java:
struct ClassA {
enum Type {
case b, c, d
}
var type: Type
}
and then use it like this:
let a = ClassA(type: .b)
In ES6, a similar construction would be:
const Type = {
a: 0,
b: 1,
c: 2,
}
class ClassA {
constructor(type) {
this.type = type
}
}
However, there are some issues from my perspective:
- To build a ClassA object, the user must also import Type
- Type doesn't feel like a natural part of ClassA
Is my code acceptable for JavaScript? Are these described problems typical in JavaScript? Is there a better approach to this issue?
The current problem I am trying to address is the following:
I have multiple types of user activities represented as objects. While they share many similarities like name, date, duration, etc., they need to be displayed differently based on their types.
My initial solution was to assign all essential fields with their respective types to each object.