Player不会在JavaScript中继承CardHolder

时间:2014-07-29 03:56:38

标签: javascript inheritance

我是第一次在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不会继承deal中的CardHolder函数。

为什么它不起作用?

2 个答案:

答案 0 :(得分:2)

问题在于

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

答案 1 :(得分:0)

当您对Object.prototype进行分配时,您正在删除最初对新分配的任何内容,因此在您对Player.prototype的分配中,您实际上是在分配constructor和{{1然后再破坏其他任何东西。试试这个:

toString