操纵实例的属性

时间:2011-08-13 05:52:40

标签: javascript constructor

这是我的代码:

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.totalcompare(),即使它们在程序的早期分配了值,并且都包含在构造函数中。有人知道这里有什么问题吗?

1 个答案:

答案 0 :(得分:1)

如果您在比较功能中修复了支撑,它似乎在我这里工作:http://jsfiddle.net/jfriend00/wnzz8/并且可以按照您的预期访问两个总值。

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

P.S。我还删除了refresh()调用,因为我不知道他们做了什么,而且你没有为他们提供代码。

相关问题