javascript - 比较 2 个独立 JavaScript 对象的类

标签 javascript comparison class-design

我想比较 2 个 JavaScript 对象的类。下面当前的调用失败。这里的想法是使用传入的“from 和 to”变量来提取正确的交叉汇率。

感谢您的帮助!

更新:工作代码现在如下所示:

<script type="text/javascript">
<!--
    // ------------------------
    // CLASS
    function Currency(clientId, country, code, imageURL, name) {

        this.clientId = clientId            //EXAMPLE: txtBudget
        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
    };
    Currency.prototype.convertFrom = function (currency, factor) {
        this.amount = currency.amount * factor;
    }

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

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

    // CLASS
    function Reais(clientId, imageURL) {
        Currency.call(this, clientId, "Brazil", "BRL", imageURL, "Reais");
    };
    Reais.prototype = new Currency();
    Reais.prototype.constructor = Reais;

    // ------------------------
    // CLASS
    function Suscriber(element) {
        this.element = element;
    };
    // CLASS
    function Publisher() {
        this.subscribers = new Array();
        this.currencyCrossRates = new Array();
    };
    Publisher.prototype.findCrossRate = function (from, to) {
        var crossRate = null;
        for (var i = 0; i < this.currencyCrossRates.length; i++) {
            if ((this.currencyCrossRates[i].from.constructor === from.constructor) && (this.currencyCrossRates[i].to.constructor === to.constructor))
                crossRate = this.currencyCrossRates[i];
        }
        return crossRate;
    }

    // ------------------------
    // CLASS
    function CurrencyCrossRate(from, to, rate) {
        this.from = from;
        this.to = to;
        this.rate = parseFloat(rate);
    };

    jQuery(document).ready(function () {

        var dollar = new Dollar(null, null);
        var reais = new Reais(null, null);

        var dollarToReais = new CurrencyCrossRate(dollar, reais, 0.8);
        var reaisToDollar = new CurrencyCrossRate(reais, dollar, 1.2);

        publisher = new Publisher();
        publisher.currencyCrossRates.push(dollarToReais);
        publisher.currencyCrossRates.push(reaisToDollar);

        // SETUP
        jQuery(".currency").each(function () {
            publisher.subscribers.push(new Suscriber(this));
        });

        var newDollar = new Dollar(null, null);
        var newReais = new Reais(null, null);

        // This now resolves correctly
        var first = crossRate = publisher.findCrossRate(newDollar, newReais);
        var second = crossRate = publisher.findCrossRate(newReais, newDollar);
    });
-->
</script>

最佳答案

instanceof 的右侧运算符不应该是原型(prototype)对象,而应该是对象的构造函数,可以通过相关对象的 constructor 属性进行访问。由于此属性实际上引用了用于构造对象的函数,因此使用通常的相等运算符进行比较:

this.currencyCrossRates[i].from.constructor == from.constructor

编辑:

  1. 删除行 Pound.prototype.constructor = Pound(); 等(每种货币各一行)。 constructor 属性是一个内置功能,它会自动引用正确的函数。然而,不幸的是,它是可写的,因此可以重新分配 - 不要这样做!

  2. 条件应采用以下形式:this.currencyCrossRates[i].from instanceof from.constructor - 左侧操作数是对象,右侧操作数是构造函数函数

关于javascript - 比较 2 个独立 JavaScript 对象的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6903121/

相关文章:

c++ - 无法根据 boolean 值结束 do/while 循环 - C++

MySQL 与 Microsoft SQL

database - DB 层成员应该是静态的还是实例的?

java - 这些类之间应该如何分配职责?

c++ - 在 C++ 中设计多维数组

javascript - 如何在表单提交时在 Google Analytics 上设置事件跟踪?

javascript - 选中/取消选中复选框时,在数组中添加/删除数字

javascript - 拼图 block 形状变化

JavaScript:测试原语的相等性

javascript - :valid and :invalid selectors in jQuery