不使用Java中的Switch(ES6)调用新构造函数类的更好方法 [英] Better way to call new constructor classes not using a Switch in Javascript (ES6)

查看:95
本文介绍了不使用Java中的Switch(ES6)调用新构造函数类的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,该类取决于字符串的类型,进入swithc语句,并调用与之相关的新"class_name"(构造函数).

但是,我想知道是否有更好的方法来解决这个问题.

如果有一种方法可以在存在的所有类的特定文件夹内独立调用构建器,那将是理想的选择.

项目示例:

  • Types/_String.js
  • Types/_Date.js
  • Types/_Integer.js
  • .....
  • TypeSwitcher.js(包含调用"Types"类的构造函数的开关).

与"TypeSwitcher.js"相关的代码:

"use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";
import Document from "./Inputs/Document";
import Container from "./Inputs/Container";

export default class InputFactory
{
    static getInput(key,typedObject,form,inputDef)
    {
           let type=typedObject.getField(key).getTypeName();
           switch (type) 
           {
             case "String": {
               return new _String(key,typedObject,form,inputDef); 
             } break;

             case "Integer": {
               return new _Integer(key,typedObject,form,inputDef); 
             } break;

             case "Date": {
               return new _Date(key,typedObject,form,inputDef);                
             }break;

             case "Document": {
              return new Document(key,typedObject,form,inputDef);                
            }break;

            case "Container": {
              return new Container(key,typedObject,form,inputDef);                
            }break;  

            default: {throw "Input undefined:"+type}     
           }          
    }       
}

新功能(已编辑) 我没有只有下划线"_"的类,这也是正常的.自从我重新定义系统的基本类型以根据需要自定义它们以来,我放置了缩进脚本(String.js是保留的,不会保留,因此是_String.js)

我试图做这样的事情:

"use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";

const predefinedSystemVar = ["String", "Integer", "Date"];

export default class InputFactory
{
    static getInput (key, typedObject, form, inputDef)
    {
      let type = typedObject.getField(key).getTypeName();
      if (predefinedSystemVar.includes(type) )    
          type = `_${type}`;  
      return eval( `new types[type](key, typedObject, form, inputDef)` );          
    }
}

已更新 但是由于webpack,它不能很好地工作. v4 :( 我认为webpack v4将名称更改为import等,并且它不起作用.

问题,如果将来我要创建一个新的类,则必须添加一个新的"case"并导入以考虑新的构造函数.

所以,我尝试过的解决方案我不太喜欢,因为我必须将新变量添加到"predefinedSystemVar" ...

ES6或javascript中是否有某些东西可以更好,更优化的方式完成这项工作,而不是使用switch-> case?

附加说明:项目环境:

  • Webpack v.4.x
  • Babel 7.x
  • ES6

package.json

{
  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.2",
    "@babel/plugin-proposal-class-properties": "^7.7.0",
    "@babel/preset-env": "^7.7.1",
    "babel-loader": "^8.0.6",
    "cypress": "^3.7.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },

解决方案

是的,如果我正确地理解了这一点,那应该是可能的.除非我犯了一些语法错误,否则下面的代码应该可以工作.它确实需要更新classes数组,但是比更新switch case和其他解决方案的工作量少.我希望这会有所帮助,如果确实有帮助,请标记为答案,祝您一整天都好,加油!

 "use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";

const classes = {
    _String,
    _Integer,
    _Date
};

export default class InputFactory
{
    static getInput(key,typedObject,form,inputDef)
    {
           let type=typedObject.getField(key).getTypeName();
           if (classes["_"+type]{
            return new classes["_"+type](key,typedObject,form,inputDef);
           } else {
            throw "Input undefined:"+type
           }         
    }       
} 

I have a class, which depends on type of string, gets in in a swithc statement, and call to de new "class_name" related (constructor).

But, I would like to know if there is a better way to solve this.

It would be ideal if there is a way to call the builder independently, within a specific folder, of all the classes that exist.

Project example:

  • Types/_String.js
  • Types/_Date.js
  • Types/_Integer.js
  • .....
  • TypeSwitcher.js (contains the switch that calls the constructors of the "Types" classes).

Associated Code to "TypeSwitcher.js":

"use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";
import Document from "./Inputs/Document";
import Container from "./Inputs/Container";

export default class InputFactory
{
    static getInput(key,typedObject,form,inputDef)
    {
           let type=typedObject.getField(key).getTypeName();
           switch (type) 
           {
             case "String": {
               return new _String(key,typedObject,form,inputDef); 
             } break;

             case "Integer": {
               return new _Integer(key,typedObject,form,inputDef); 
             } break;

             case "Date": {
               return new _Date(key,typedObject,form,inputDef);                
             }break;

             case "Document": {
              return new Document(key,typedObject,form,inputDef);                
            }break;

            case "Container": {
              return new Container(key,typedObject,form,inputDef);                
            }break;  

            default: {throw "Input undefined:"+type}     
           }          
    }       
}

New (edited) I don't have classes only with an underscore "_", also normal. And I have put indent script since I am redefining the basic types of the system to customize them to my need (String.js is reserved and does not leave, so _String.js)

I tried to do something like that:

"use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";

const predefinedSystemVar = ["String", "Integer", "Date"];

export default class InputFactory
{
    static getInput (key, typedObject, form, inputDef)
    {
      let type = typedObject.getField(key).getTypeName();
      if (predefinedSystemVar.includes(type) )    
          type = `_${type}`;  
      return eval( `new types[type](key, typedObject, form, inputDef)` );          
    }
}

Updated But it didnot work well, because of webpack. v4 :( I think webpack v4 changes names to imports, etc., and it does not work the code trick ...

Problem, if I create a new class in the future, I would have to add a new "case" and import to contemplate the new constructor.

So, the solution I tried, I dont like so much, because I'd have to add new variables to "predefinedSystemVar"...

Is there something in ES6 or javascript to get this work in a better and optimized way instead of switch -> case?

Additional note: Project enviroment:

  • Webpack v.4.x
  • Babel 7.x
  • ES6

package.json

{
  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.2",
    "@babel/plugin-proposal-class-properties": "^7.7.0",
    "@babel/preset-env": "^7.7.1",
    "babel-loader": "^8.0.6",
    "cypress": "^3.7.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },

解决方案

Yes, if I am understanding this correctly it should be possible. The below code should work, unless I made some syntactical error. It does require the updating of a classes array, but is less work than updating the switch case and the other solution. I hope this helps and if it does please mark as the answer, have a good rest of your day, cheers!

"use strict"
import _String from "./Inputs/_String";
import _Integer from "./Inputs/_Integer";
import _Date from "./Inputs/_Date";

const classes = {
    _String,
    _Integer,
    _Date
};

export default class InputFactory
{
    static getInput(key,typedObject,form,inputDef)
    {
           let type=typedObject.getField(key).getTypeName();
           if (classes["_"+type]{
            return new classes["_"+type](key,typedObject,form,inputDef);
           } else {
            throw "Input undefined:"+type
           }         
    }       
}

这篇关于不使用Java中的Switch(ES6)调用新构造函数类的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆