2011-08-01 4 views
0

2 개의 JavaScript 객체 클래스를 비교하고 싶습니다. 아래의 현재 호출이 실패합니다. 여기에서 전달 된 "from 및 to"변수를 사용하여 올바른 교차 속도를 추출하는 것이 좋습니다.2 개의 별도 JavaScript 객체 클래스 비교

도움 주셔서 감사합니다.

업데이트 : instanceof의 오른쪽 운영자가 constructor 속성을 통해 액세스 할 프로토 타입 객체 대신 객체의 생성자 함수, 안

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

     var stop = ""; 
    }); 
--> 
</script> 
+0

instanceof가 작동해야합니다. 비교하면 무엇을 의미합니까? –

+0

나는 crossRate.from 인스턴스를 전달 된 인스턴스와 비교하려고합니다. –

+1

비교에서 프로토 타입을 생성자로 변경해보십시오. – Joe

답변

2

: 작업 코드는 이제 다음과 같습니다 문제의 객체의

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

편집 :

  1. 라인을 Pound.prototype.constructor = Pound(); 등 (하나를 제거 실제로 참조에서이 속성은 개체를 생성하는 데 사용 된 기능을 때문에, 비교는 보통 평등 연산자로 이루어집니다 각 통화에 대해). constructor 속성은 올바른 기능을 자동으로 참조하는 기본 제공 기능입니다. 그러나, 은 불행하게도 쓰기가 가능하므로 다시 할당 할 수 있습니다 -하지 마세요!!

  2. 조건의 형식은 this.currencyCrossRates[i].from instanceof from.constructor이어야합니다. 왼쪽 피연산자는 Object이고 오른쪽은 생성자 함수입니다.

+0

업데이트 됨! 그것은 여전히 ​​실패하므로 모든 것을 게시했습니다. 도와 주셔서 감사합니다. –

+0

통화 기초 클래스 할당 때문에 "Pound.prototype.constructor"를 덮어 써야한다고 생각합니다. –

+0

오, 그래, 네 말이 맞아. 그래서 할당은'()'없이'Pound.prototype.constructor = Pound; '가되기를 원합니다. –