Player不能在JavaScript中继承CardHolder

Player won't inherit from CardHolder in JavaScript

本文关键字:继承 CardHolder JavaScript 不能 Player      更新时间:2023-09-26

我第一次在JavaScript中练习OOP,不明白为什么继承不起作用。

代码:

function Card(s, v) {
    if (arguments.length === 0) {
        this.suit = SUITS[Math.floor(Math.random()*SUITS_LENGTH)];
        this.val = VALS[Math.floor(Math.random()*VALS_LENGTH)];
    }
    else {
        this.suit = s;
        this.val = v;
    }
}
Card.prototype = {
    constructor: Card,
    toString: function() {
        return this.val + " of " + this.suit;
    },
    lowVal: function() {
        if (this.val === "A") { return 1; }
        else if (this.val === "J" || this.val === "Q" || this.val === "K") { return 10; }
        else { return parseInt(this.val); }
    },
    highVal: function() {
        if (this.val === "A") { return 11; }
        else if (this.val === "J" || this.val === "Q" || this.val === "K") { return 10; }
        else { return parseInt(this.val)}
    }
};
function CardHolder() {
    this.status = "in";
    this.cards = [];
}
CardHolder.prototype = {
    constructor: CardHolder,
    deal: function() {
        this.cards.push(new Card());
    },
    lowVal: function() {
        var lowVal = 0;
        for (var i = 0, len = this.cards.length; i < len; i++) {
            lowVal += this.cards[i].lowVal();
        }
        return lowVal;
    },
    highVal: function() {
        var highVal = 0;
        for (var i = 0, len = this.cards.length; i < len; i++) {
            highVal += this.cards[i].highVal();
        }
        return highVal;
    },
    score: function() {
        if (this.highVal() > 21) { return this.lowVal(); }
        else { return this.highVal(); }
    }
};
function Player(id) {
    CardHolder.call(this);
    if (typeof(id)) {
        this.id = id;
    }
}
Player.prototype = Object.create(CardHolder.prototype);
Player.prototype = {
    constructor: Player,
    toString: function() {
        var returnString = "Player " + this.id + ":'n";
        for (var i = 0, len = this.cards.length; i < len; i++) {
            returnString += this.cards[i].toString() + "'n"
        }
        return returnString;
    }
}

var p = new Player();
p.deal();
console.log(p.toString());

输出Uncaught TypeError: undefined is not a function。我认为这意味着p没有继承CardHolderdeal函数。

为什么不工作?

问题是

Player.prototype = {
    constructor: Player,
    toString: function() {
        var returnString = "Player " + this.id + ":'n";
        for (var i = 0, len = this.cards.length; i < len; i++) {
            returnString += this.cards[i].toString() + "'n"
        }
        return returnString;
    }
}

正在覆盖

中分配给Player.prototype的值
Player.prototype = Object.create(CardHolder.prototype);

要避免这种情况,可以这样做:

Player.prototype = Object.create(CardHolder.prototype);
Player.prototype.constructor = Player;
Player.prototype.toString = function() {
        var returnString = "Player " + this.id + ":'n";
        for (var i = 0, len = this.cards.length; i < len; i++) {
            returnString += this.cards[i].toString() + "'n"
        }
        return returnString;
};

当你给Object.prototype分配任务时,你是在用新任务淘汰它最初拥有的任何东西,所以在你给Player分配的任务中。在原型中,你实际上是在分配constructortoString,但随后会淘汰其他任何内容。试试这个:

Player.prototype = Object.create(CardHolder.prototype);
Player.prototype.constructor = Player; //adding constructor, not clobbering the rest of prototype
Player.prototype.toString=function() { //adding toString, not clobbering the rest of prototype
        var returnString = "Player " + this.id + ":'n";
        for (var i = 0, len = this.cards.length; i < len; i++) {
            returnString += this.cards[i].toString() + "'n"
        }
        return returnString;
    };
var p = new Player();
p.deal();
console.log(p.toString());