JavaScript 对象创建

发布于 2024-11-30 20:36:18 字数 486 浏览 0 评论 0原文

这里是 JavaScript 新手,我在工作中浏览一些 js 代码时遇到了一个用于创建对象的辅助函数,它是这样的,

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... >
}

虽然这确实完成了工作,但我想知道是否有更好、更简洁的编写方式这。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-12-07 20:36:18

你是对的,过多的 if..thenswitch 逻辑是 代码味道并且几乎总是可以重构为更优雅的东西。在这种情况下,基于名称的工厂可以重构为字典,其中键作为该名称,值作为返回的函数

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);
}

实时示例: http://jsfiddle.net/KkMnd/

编辑: createElement 方法中的该行可以/应该首先检查是否为传入的 TYPES.* 。一个好的方法是在尝试调用该方法之前检查字典中是否存在元素。

return (typeof dictionary[name] == 'function') ? new dictionary[name](data) : some_default_value;

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;
牛↙奶布丁 2024-12-07 20:36:18

使用 switch 语句会更清晰一些,但语义上是相同的。

function createElement(name,data){
switch(name)
{
case TYPES.TEXT:
  return new Text(data)
  break;
case TYPES.WORD:
  return new WORD(data)
  break;
default:
  // etc. code to be executed if no values match
}
}

It would be a bit cleaner but semantically the same to use a switch statement.

function createElement(name,data){
switch(name)
{
case TYPES.TEXT:
  return new Text(data)
  break;
case TYPES.WORD:
  return new WORD(data)
  break;
default:
  // etc. code to be executed if no values match
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文