1

I used to create classes with type property in usual OOP languages like swift, or java:

struct ClassA { enum Type { case b, c, d } var type: Type } 

and then use it like

let a = ClassA(type: .b) 

In es6 similar construction will be:

const Type = { a: 0, b: 1, c: 2, } class ClassA { constructor(type) { this.type = type } } 

Issues with this from my perspective are next:

  • To build a ClassA object user has to import Type as well
  • Type doesn't seem like a part of ClassA

Is my code is okay for Javascript? Described problems are okay in Javascript? Is there any better approach for this?

Current problem I'm trying to solve is next

I have several types of user activities represented as objects. They share lots of similarities like name, date, duration etc., but have to be displayed depending on their types.

My solution was to give all objects essential fields with their types.

6
  • You can make Type a property of ClassA, but that’s not really any better.
    – Ry-
    CommentedOct 15, 2017 at 7:41
  • @Ryan, exactly, thought about this as wellCommentedOct 15, 2017 at 7:42
  • but in this case object-enum will be at least a part of ClassACommentedOct 15, 2017 at 7:43
  • Don't let the ES6 class syntax fool you. This is syntax sugar. There are no classes in Javascript. Never have been, never will be.
    – Tomalak
    CommentedOct 15, 2017 at 7:59
  • Instead of trying to emulate specific behavior of other languages, explain the problem you want to solve.
    – Tomalak
    CommentedOct 15, 2017 at 8:07

2 Answers 2

1

Correct way to implement object with enum in javascript

There is no correct way for that, as there is nothing comparable to enums in JS. You can only take more and more efforts to simulate the behaviour as closely as possible to the one you know. But to me, then you're ignoring the language you're developing in, and only add bloat just to keep your precious little development routine and don't need to learn something new. (That's not against you personally. I've just seen that before.)

I'd simply include Type as constants into ClassA

class ClassA { constructor(type) { this.type = type } } Object.defineProperties(ClassA, { TYPE_A: { enumerable: true, value: 0}, TYPE_B: { enumerable: true, value: 1}, TYPE_C: { enumerable: true, value: 2}, }); let instance = new ClassA( ClassA.TYPE_A ); 

Or you could take a look at Typescript wich has enums at compile-time.

instead of simulation of enums is there a better approach to solve the problem described in my question?


I have several types of user activities represented as objects. They share lots of similarities like name, date, duration etc., but have to be displayed depending on their types.

My solution was to give all objects essential fields with their types.

Can't give you a good answer here, as this is still too broad. I'm not sure, "activities" sounds like behaviour, but telling by the properties I'd rather think that we're just talking about data ... and how they are processed.

I'm not sure if I'd even implement a class for that and not just use plain object literals/json. And to make it more readable, convert the type into a string.

3
  • seems like a nice solution for my issueCommentedOct 15, 2017 at 8:31
  • thank you for your clarification. one more question - instead of simulation of enums is there a better approach to solve the problem described in my question?CommentedOct 15, 2017 at 8:40
  • @VladHatko I've updated my answer to address that as well.
    – Thomas
    CommentedOct 15, 2017 at 8:54
1

Classically situation is solved like this:

class ClassA { constructor(type) { this.type = type; } } ClassA.TYPE_A = 0; ClassA.TYPE_B = 1; ClassA.TYPE_C = 2; var instance = new ClassA(ClassA.TYPE_B); 

You can use Object#defineProperty do define fixed, read-only properties instead of normal ones, if you really want to.


The equivalent code in ES3 syntax is shorter and more to the point, IMHO.

function ClassA(type) { this.type = type; } ClassA.TYPE_A = 0; ClassA.TYPE_B = 1; ClassA.TYPE_C = 2; var instance = new ClassA(ClassA.TYPE_B); 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.