How do I create a new object in javascript based on a type-string?

后端 未结 3 1451
臣服心动
臣服心动 2021-02-14 01:44

How do I create a new object in javascript based on a variable type-string (containing the name of the object)?

Now I have: (with more tools coming the list will get lon

3条回答
  •  灰色年华
    2021-02-14 01:54

    You should consider rethinking your approach. It would be better to have something like a Tools object, which would have the tools as properties, like

    Tools = {"SelectTool": SelectTool /* etc */}`.
    

    This way, you could access the tools both as new Tools.SelectTool and new Tools[var_with_tool_name].

提交回复
热议问题