javascript - Enum using empty objects -
we can create "enums" in javascript follows:
var myenum = { a: 0, b: 1, }
can use empty objects instead of numbers follows?
var myenum = { a: {}, b: {}, }
what's difference , should used? there isn't specific use case.
i changed answer after edited because noticed trying.
yes can use objects enum value without problem. when define {}
in object, creates , empty object unique reference.
{} != {}
won't equal because 2 different objects same doesn't mean same object. 2 red balls have, these balls same ball? no.
but instance.type == myenum.objectenum1
true. because both instance , myenum object shares same reference object.
var myenum = { a: 1, b: 2, c: 3, objectenum1: {}, objectenum2: {} } var obj1 = { type: myenum.b } var obj2 = { type: myenum.c } var obj3 = { type: myenum.objectenum1 } console.log(obj1.type == myenum.b); //should true console.log(obj2.type == myenum.a); //should false console.log(obj2.type == myenum.c); //should true console.log(obj3.type == myenum.objectenum1); //should true console.log(obj3.type == myenum.objectenum2); //should false
Comments
Post a Comment