Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:34:33+00:00 2026-05-24T07:34:33+00:00

I want to compare the classes of 2 JavaScript objects. The current call below

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:34:34+00:00Added an answer on May 24, 2026 at 7:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compare the current value of an in-memory Hibernate entity with the
I want to be able to call methods on CLR classes from C++. In
In Jquery, I want to compare the classes of an h3 and a div.
I want to write a function that accepts two objects as parameters and compare
I want to compare two Java objects without overriding equals method . Since I
I want to compare two objects that are derived from the same base type
I want to compare two ms-access .mdb files to check that the data they
I want to compare and contrast the various source control systems out there. Any
I want to compare the first few bytes in byte[] with a string. How
I want to compare records between 2 databases on my SQL 2008 Server. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.