操纵实例的属性

发布于 2024-11-29 15:36:08 字数 2281 浏览 0 评论 0原文

这是我的代码:

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.totaltl_placemat.total < code>compare(),即使它们在程序中较早地被赋值并且都包含在构造函数中。有谁知道这里出了什么问题吗?

Here is my code:

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

I make two instances of the Constructor here:

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

And I call them here:

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

For some reason, I cannot access d_placemat.total and tl_placemat.total in compare(), even though they are assigned values earlier in the program and are both contained within the Constructor. Does anyone know what is wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-12-06 15:36:08

如果您修复比较函数中的支撑,它似乎对我有用: http://jsfiddle.net /jfriend00/wnzz8/ 并且可以访问您所期望的两个总值。

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

PS 我还删除了刷新()调用,因为我不知道它们做了什么,并且您没有为它们提供代码。

If you fix up the bracing in your compare function, it seems to work for me here: http://jsfiddle.net/jfriend00/wnzz8/ and there is access to the two total values as you are expecting.

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

P.S. I also removed the refresh() calls because I don't know what they did and you didn't provide the code for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文