JavaScript中的动态实例化

JavaScript中的动态实例化,javascript,object,Javascript,Object,我有一个下拉列表,其中包含可以实例化为JavaScript“类”的货币分类。我目前使用switch语句来实现这一点,但我绝对相信有一种更雄辩的方式来实现这一点。那么,有谁能给我指一条更好的路吗 动态实例化类有更好的方法吗?: function ddlCurrency_selectedIndexChanged() { var currency = null; switch (this.value) { case "Dollar": cur

我有一个下拉列表,其中包含可以实例化为JavaScript“类”的货币分类。我目前使用switch语句来实现这一点,但我绝对相信有一种更雄辩的方式来实现这一点。那么,有谁能给我指一条更好的路吗

动态实例化类有更好的方法吗?:

function ddlCurrency_selectedIndexChanged() {

    var currency = null;

    switch (this.value) {
        case "Dollar":
            currency = new Dollar(null);
            break;
     case "Reais":
            currency = new Reais(null);
                break;
    }

    // Do something with the class here
};
以下是课程:
以防你想见他们

// ------------------------
// CLASS - Base Class
function Currency(country, code, imageURL, name) {
    this.country = country;     //EXAMPLE: America
    this.code = code;   //EXAMPLE: USD
    this.imageURL = imageURL;   //EXAMPLE: "http://someplace/mySymbol.gif"
    this.name = name;   //EXAMPLE: Dollar
    this.amount = parseFloat("0.00");   //EXAMPLE: 100
};

// CLASS
function Pound(imageURL) {
    Currency.call(this, "Greate Britain", "GBP", imageURL, "Pound");
};
Pound.prototype = new Currency();
Pound.prototype.constructor = Pound;

// CLASS
function Dollar(imageURL) {
    Currency.call(this, "America", "USD", imageURL, "Dollar");
};
Dollar.prototype = new Currency();
Dollar.prototype.constructor = Dollar;

// CLASS
function Reais(imageURL) {
    Currency.call(this, "Brazil", "BRL", imageURL, "Reais");
};
Reais.prototype = new Currency();
Reais.prototype.constructor = Reais;
更新:
使用
eval()。奇怪的是,我看到人们投票反对使用它……尽管我不知道为什么。就个人而言,我倾向于更喜欢它,因为您可能不会从
窗口
对象中得到任何东西。这方面的一个很好的例子是,某些AMD样式的异步加载对象……它们不会挂起
窗口

使用eval的示例:

var currency = eval('new Dollar()');

假设顶层对象是窗口(如果您在浏览器中):


这是可行的,因为您的所有类都只是全局对象的属性,并且
window[property]
检索属性。

如果没有window,我该怎么做?刚刚找到它,但是使用eval()是否被认为是邪恶的?我们可以在node.js环境中这样做吗?可能是
currency = new window[this.value](null);