Player不能在JavaScript中继承CardHolder
Player won't inherit from CardHolder in JavaScript
我第一次在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
没有继承CardHolder
的deal
函数。
为什么不工作?
问题是
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分配的任务中。在原型中,你实际上是在分配constructor
和toString
,但随后会淘汰其他任何内容。试试这个:
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());
相关文章:
- 从控制器继承了隔离的作用域以生成可重用的指令
- 两个指令创建新的继承的和隔离的作用域-元素得到哪个
- 当使用控制器作为语法时,如何从父指令继承属性
- 以jquery方式继承Javascript
- JavaScript对象不是从原型链继承的
- 使用Object.create()的角度服务继承
- 数据与Javascript中的继承冲突
- JavaScript对象继承问题
- Javascript嵌套函数属性继承
- 为什么不't htmlCollection从数组继承
- 继承的属性检查
- 没有预定义宽度的固定元素最初放在引导容器中,不会从父元素继承宽度
- 如何使用原型继承编写一个整洁灵活的复杂javascript应用程序
- 使用技巧在javascript中强制执行私有继承
- $emit,$broadcast,原型继承
- 如何检查DOM节点是否继承自构造函数
- Javascript多重继承模式
- 玉中继承模板的修改
- Angular中的作用域继承$broadcast
- Player不能在JavaScript中继承CardHolder