Я хочу сравнить классы 2 объектов JavaScript.Текущий вызов ниже не удается.Идея здесь состоит в том, чтобы получить извлечение правильного кросс-курса с использованием переданных переменных «от и до».
Спасибо за помощь!
ОБНОВЛЕНО: рабочий кодВыглядит так:
<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>