比较 2 个独立 JavaScript 对象的类

发布于 11-27 03:52 字数 3256 浏览 0 评论 0原文

我想比较 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>

I want to compare the classes of 2 JavaScript objects. The current call below fails. The idea here is to deal extract the correct cross-rate using the passed-in "from and to" variables.

Thanks for the help!

UPDATED: The Working Code Now Looks Like This:

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

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

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

发布评论

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

评论(1

最初的梦2024-12-04 03:52:30

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

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

编辑:

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

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

The right hand side operator of instanceof should not be the prototype object, but instead the object's constructor function, accessible through the constructor property of the object in question. Since this property in fact references the function which was used to construct the object, comparison is done with the usual equality operator:

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

EDIT:

  1. Remove the lines Pound.prototype.constructor = Pound(); etc (one for each currency). The constructor property is a built-in feature which automatically references the correct function. However, it is unfortunately writable and so can be reassigned - don't do it!

  2. The conditions should be of the form this.currencyCrossRates[i].from instanceof from.constructor - the left operand is the Object and the right one is the constructor function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文