Vladyslav Zavalykhatko
Vladyslav Zavalykhatko

Reputation: 17374

Correct way to implement object with enum in javascript

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:

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.

Upvotes: 1

Views: 3272

Answers (2)

Tomalak
Tomalak

Reputation: 338228

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);

Upvotes: 1

Thomas
Thomas

Reputation: 12637

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.

Upvotes: 1

Related Questions