манипулирование свойствами экземпляров - PullRequest
0 голосов
/ 13 августа 2011

Вот мой код:

var Placemat = function(id,id2,id3) {
    this.bucket = "";
    this.result = 0;
    this.bucketvalue = 0;
    this.total = 0;
    this.light = 0;
    this.deal = function() {
        var card1 = deck.shift();
        var card2 = deck.shift();
        this.hand = document.getElementById(id2);
        this.hand.innerHTML = card1.num + " of " + card1.suit + ", " + card2.num + " of " + card2.suit;

        if (card1.num == "Ace" && card2.value == "Ace") {
            card1.value = 11;
        }

        this.result = this.result + card1.value + card2.value;
        this.total = this.result;

        totalplayer = totalplayer + 1;

        return this.total;
    };
    this.hit = function() {
        this.light++;
        var card3 = deck.shift();
        var nhand = ", " + card3.num + " of " + card3.suit;
        this.bucket = this.bucket + nhand;
        this.bucketspace = document.getElementById(id3);
        this.bucketspace.innerHTML = this.bucket;
        this.bucketvalue = this.bucketvalue + card3.value;
        this.total = this.total + this.bucketvalue;

        if (this.total > 21) {
            totaldone = totaldone + 1;

            alert("Bust! By " + nhand);
            refresh();
        };

        if (this.light == 3) {
            alert("5 Card Blackjack! You win!");
            refresh();
        };

        return this.total;
    };
    this.stay = function() {
        this.player = document.getElementById(id);
        this.player.style.backgroundColor = "gray";
        this.player.innerHTML = "Stayed at " + this.total;

        totaldone = totaldone + 1;
    };
};

Я делаю два экземпляра Конструктора здесь:

var d_placemat = new Placemat(param1,param2);
var tl_placemat = new Placemat(param3,param4);

И я называю их здесь:

function compare() {
            if (d_placemat.total > tl_placemat.total) {
                alert("Dealer beats TL!");
                refresh();
            }
            else {
                alert("TL beats Dealer!");
                refresh();
            }
        };
    };

По какой-то причине я не могу получить доступ к d_placemat.total и tl_placemat.total в compare(), даже если им ранее присвоены значения в программе, и оба они содержатся в конструкторе.Кто-нибудь знает, что здесь не так?

1 Ответ

1 голос
/ 13 августа 2011

Если вы исправите привязку в своей функции сравнения, мне кажется, она сработает здесь: http://jsfiddle.net/jfriend00/wnzz8/ и есть доступ к двум итоговым значениям, как вы ожидаете.* PS Я также удалил вызовы refresh (), потому что я не знаю, что они сделали, а вы не предоставили для них код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...