DEV Community

Discussion on: Replace your switch statement and multiple "if and else", using Object Literals - [en-US].

Collapse
 
nikitahl profile image
Nikita Hlopov

Nice article and interesting approach. Thanks!
I have a question however. Why would you use methods to return the string instead of assigning this string to a property like so:

const Users = {
  admin: 'This User is Admin!',
  client: 'This User is Client!',
  salesman: 'This User is Salesman!',
  default: 'This User is MasterUser'
}

I mean wouldn't it be more clean and straightforward way?

Collapse
 
akolybelnikov profile image
Andrey Kolybelnikov

I was about to comment with the same when I saw this has already been suggested :)
One more cool little enhancement will allow you to adhere to immutability principle (that is if you'd like to prevent anyone from changing your object properties somewhere in the code later):

Object.freeze(Users)

From now on your object literal is readonly.

Collapse
 
tauantcamargo profile image
Tauan Tathiell

hey man thanks for the comment :D

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Hey man ... thanks .. so in the project that i'm current working i used methods to change the data and other things ... it was just a mistake hehehe i'm sorry .. your approach is absolutelly correct... thanks for your review i'm going to edit it ..