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 8714887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:42:45+00:00 2026-06-13T05:42:45+00:00

I have something like this… define([‘ClassA’, ‘ClassB’, ‘ClassC’], function(ClassA, ClassB, ClassC) { return {

  • 0

I have something like this…

define(['ClassA', 'ClassB', 'ClassC'], 
  function(ClassA, ClassB, ClassC) 
  {
    return {
      build: function(className) {
        var obj;
        switch(className)
        {
            case 'ClassA': obj = new ClassA(); break;
            case 'ClassB': obj = new ClassB(); break;
            case 'ClassC': obj = new ClassC(); break;
        }
        return obj;
    }
  }
}

This seems ok but is there a better way to write it? I tried replacing switch to

return new arguments[className]();    // doesn't work

The closest I can get to is to use a map:

var classes = {
    ClassA: ClassA,
    ClassB: ClassB,
    ClassC: ClassC
}
return new classes[className]();

Is there a better way?

  • 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-06-13T05:42:47+00:00Added an answer on June 13, 2026 at 5:42 am

    There’s really no problem with your object there.
    It’s fast, efficient and easy to follow.
    The only suggestion I’d have is to create a var to hold the value, or to test more-explicitly if className is a suitable string, and is in fact in your list:

    Either:

    var construct = classes[className];
    if (construct) { return new construct(); }
    else { /* handle the case where the class doesn't exist */ }
    

    Or:

    return classes[className] && (new classes[className]()) || null;
    

    The second checks for classes[className] and should return a new instance if it exists (JS returns the value on the very right of AND)…
    OR it will return null.

    Maybe null isn’t what you want.
    But the point is that you should be prepared to handle somebody passing: "Bob" to your factory, despite the fact that there is no Bob class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: function showFunction () { // need position and place
I have something like this: $.getJSON('/scripts/commons/theScriptDoTravelBackInTime.php',{ }, function(){ // etc.. etc... } }); Is
I have something like this: <a href=# id=lol>LOL</a> <script type=text/javascript> $('#lol').click(function(){ $.get(<?php echo $host.'/index.php'?>,
I have something like this: var Something = function(){ this.render = function(){}; $(window).resize(function(){ this.render();
I have something like this: public bool Function(IEnumerable<MyObject> MyObj) { var context = new
I have something like this <button onclick=JavascriptFunction();>HTTPPost Button</button> <script type=text/javascript language=javascript> function JavascriptFunction() {
I have something like this: class Base { public: static int Lolz() { return
I have something like this: $('element.selector').live(click, function (){ run_some_func (); }); $('element.selector2').live(click, function (){
I have something like this: typedef int customType[10]; And I want a function like
I have something like this: dog = function () { this.age; this.name; this.dog1 =

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.