Javascript - 不是函数,Prototype

时间:2018-01-24 19:29:24

标签: javascript oop vue.js typeerror

我目前正在为大学项目开发​​一款多人二十一点游戏。

不幸的是我收到错误“TypeError:card.getImagemCarta()不是函数......”

我的服务器拥有一个游戏列表。每场比赛都有持有Baralho的球员,这是葡萄牙语中的“手”。每个baralho都有一系列卡片

这是我的班级卡:

class Carta {
    get hidden() {
        return this._hidden;
    }

    constructor(__valor, __naipe) {
        this.naipe = __naipe;
        this.valorCarta = __valor;
        this.pontos = this.getValorCarta();
        this._hidden = true;
    }

    clear() {
        this.naipe = -1;
        this.valorCarta = -1;
        this.pontos = -1;
    }

    showCard() {
        this._hidden = false;
    }

    hideCard() {
        this._hidden = true;
    }

    toString() {
        var s = "";
        switch (this.valorCarta) {
            case 1: s += "Ás"; break;
            case 11: s += "Valete"; break;
            case 12: s += "Rainha"; break;
            case 13: s += "Rei"; break;
            default: s += this.value; break;
        }
        s += " de ";
        switch (this.naipe) {
            case 1: s += "Paus"; break;
            case 2: s += "Ouros"; break;
            case 3: s += "Copas"; break;
            case 4: s += "Espadas"; break;
        }
        return s;
    }

    //retrieving the value of the cards from the deal function
    getValorCarta() {
        var valor = this.valorCarta;
        if (valor % 13 === 0 || valor % 13 === 11 || valor % 13 === 12) {
            return 10;
        }
        if (valor % 13 === 1) {
            return 11;
        }
        return valor % 13;
    }

    getImagemCarta() {
        var s = '';
        switch (this.naipe) {
            case 1: s += "p"; break;
            case 2: s += "o"; break;
            case 3: s += "c"; break;
            case 4: s += "e"; break;
        }
        s += this.valorCarta;
        return s;
    }
}
module.exports = Carta;

当我尝试列出所有玩家及其卡片时,在我的客户端

<div v-for="player of this.game.players" >
                <h4>{{player.playerName}}</h4>
                <h6>Valor: {{ player._pontos }}</h6>
                <div v-for="card of player._mao">
                    <img v-bind:src="pieceImageURL(card.getImagemCarta())">
                </div>
            </div>

在我的vue方法中,我有一个pieceImageURL,它返回卡片图像的正确路径。

methods: {
        closeGame (){
            this.$parent.close(this.game);
        },

        pieceImageURL: function(imgSrc) {
            return 'img/' + imgSrc + '.png';
        },
        clickPiece: function (index){
            if(!this.game.gameEnded){
                if(this.game.playerTurn != this.ownPlayerNumber){
                    alert("It's not your turn to play.");
                }else{

                    if(this.game.board[index]===0){
                        this.$parent.play(this.game,index);
                    }
                }
            }
        },

当我检查卡对象是什么时,我看到所有参数都是正确的但原型没有方法,所以当我尝试调用方法时它们不存在。这不是第一次发生在我身上,但我无法理解并解决它。

就好像我放弃了原型中的信息一样。

我错了吗?

非常感谢。

0 个答案:

没有答案