I have a class hierarchy like:
|-> Square
AbstractShape -+-> Circle
|-> Triangle
Now, I'd like to implement strategy pattern and create an object of class that's stored in string. In PHP I'd use:
$type = 'Square';
$obj = new $type();
Is there an equivalent in Node.js?
A safe way would be defining a factory object:
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]();