2017-12-18 145 views
7

指定我有一个类层次结构像类名的对象:的Node.js - 创建变量

   |-> Square 
AbstractShape -+-> Circle 
       |-> Triangle 

现在,我想实现的战略格局,并创建一个储存在String类的一个对象。在PHP中,我会使用:

$type = 'Square'; 
$obj = new $type(); 

Node.js中是否有等价物?

+0

它就像调用'new Square()' – zabusa

回答

4

如果你希望有一个更强大和可测试的方式去,你可以使用类和工厂模式的组合来发布对象。看看下面的内容,你会发现,通过这种设置,包括更细粒度的逻辑和测试的方式将变得更加容易,并为您提供更大的灵活性。你也在抽象出.issue电话背后的新对象 - 这在某些情况下可能是有益和方便的。

我也注意到你提到你的PHP背景,所以我还展示了一些ES6中面向对象的方法。

class AbstractShape { 
    constructor(type) { 
    this.type = type; 
    } 

    getType() { 
    console.log(`I am a ${this.type}`); 
    } 
} 

class Square extends AbstractShape { 
    constructor(type) { 
    super(type); 
    this.sides = 4; 
    } 

    getDescription() { 
    console.log(`I have ${this.sides} equal sides`); 
    } 
} 

class ShapeFactory { 
    static issue(type) { 
    switch(type) { 
     case 'Square': return new Square(type); 
     break; 
     case 'Circle': /* same pattern with a Circle class */ 
     break; 
    } 
    } 
} 

let shape = ShapeFactory.issue('Square'); 

shape.getType();  /* I am a Square */ 
shape.getDescription(); /* I have 4 equal sides */ 

JSFiddle Link - 演示


此外,如果你想要的东西多一点故障不是处理例如冗余容错串'Square' - there are some creative ways可以利用枚举类方法来进一步优化。我将在这里保存房地产,而不是重新编码代码片段,但将包括一个小提琴让你检查。

JSFiddle Link - 枚举的方法演示

2
  1. 快速和肮脏的方法是使用eval。但是,强烈不推荐的,因为很多的原因 - 安全,性能,可读性,可支持

    function MyType() { 
    } 
    
    var typeName = 'MyType'; 
    var typeObj = eval('new ' + typeName + '()'); 
    
  2. 更安全,更正确的比eval是使用字符串名称映射到类型(感谢@GaloisGecko)

    function createType(name) { 
        var types = { 
        "Square": Square, 
        "Circle": Circle, 
        "Triangle": Tringle 
        }; 
        return types.hasOwnProperty(name) ? new types[name]() : null; 
    } 
    
  3. 最后,最好的和明智的决定是应用工厂模式。见@scniro answer。你也可以找到很好的说明和示例here

+0

只要有可能就不要使用'eval'。在这里你不需要,所以远离这个解决方案。 –

4

安全的方式将定义一个工厂对象:

function Square() { 
} 

// here other constructors for Circle and Triangle 

var factory = { 
    "Square": Square, 
    "Circle": Circle, 
    "Triangle" : Triangle 
} 

var typeName; 

// here some code which sets typeName 

var typeObj = new factory[typeName](); 
1

经仔细考虑有周围Node.js的相当简单的方法 当你以最简单的方式实例化一个对象时,你实际上编写了new <variableName>,其中variableName是一些在某些模块中定义和导出的函数或类的主体。把这个函数/类分配给变量你require()吧。

所以,与其

const type = 'Square'; 
const aSquare = new type(); 

你需要写:

const type = 'Square'; 
const shape = require(`${pathToShapeModules}/${type}.js`); 
const aShape = new shape(); 

小缺点是eslint抱怨(在某些规则的设置),其require s为要放置在顶部模块。当然,它需要通过try ... catch等适当的异常处理,所以可能Factory解决方案更好(所以我会接受它),但我认为对于小型专业案例,这种解决方案是可以的。