Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:34:35+00:00 2026-05-25T01:34:35+00:00

JavaScript newbie here, I was going through some js code at work when i

  • 0

JavaScript newbie here, I was going through some js code at work when i came across a helper function for object creation, which went like this

createElement = function(name, data){
    if(name == TYPES.TEXT){
    return new Text(data);
    }
    else if(name == TYPES.WORD){
    return new Word(data);
    }
    else if(name == TYPES.PARAGRAPH){
    return new Paragraph(data); 
    }
    else if(name == TYPES.TABLE){
    return new Table(data);
    }
    <list goes on and on and on... >
}

while this does get the job done i would like to know if there is a better, cleaner way of writing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:34:36+00:00Added an answer on May 25, 2026 at 1:34 am

    You’re right, excessive if..then or switch logic is a code smell and can almost always be refactored into something more elegant. In this case, a factory based upon a name can be refactored into a dictionary with key as that name and value as the function to return

    var dictionary = {};
    dictionary[TYPES.TEXT] = Text;
    dictionary[TYPES.WORD] = Word;
    dictionary[TYPES.PARAGRAPH] = Paragraph;
    dictionary[TYPES.TABLE] = Table;
    
    createElement = function(name, data){
        return new dictionary[name](data);
    }
    

    Live example: http://jsfiddle.net/KkMnd/

    EDIT: That line in the createElement method could/should first check that something is configured for the TYPES.* passed in. A good way is to check that there is an element in the dictionary before trying to call that method.

    return (typeof dictionary[name] == 'function') ? new dictionary[name](data) : some_default_value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A JavaScript newbie here. I have this following code: function testObject(elem) { this.test =
I came across this Q/A on javascript code organisation. var DED = (function() {
Javascript newbie here, just trying to tweak this bit of code. It checks for
javascript newbie here, I wrote a form in html and submitted it with an
Bit of a JavaScript newbie here - I am firing this basic bit of
I have a little bit of Javascript that almost works correctly. Here's the code:
I'm a mostly-newbie Javascript'er and I'm trying it on a webpage which, upon pressing
i'm checking out some newbie Google Maps API tutorial code. They have an example
Hey all, Newbie here, so please forgive my approach and specifics. I'd appreciate some
So I'm a complete newbie and stuck on a conditional statement. Here's my code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.