访问传递给原型函数的值

时间:2017-01-23 01:00:40

标签: javascript prototype

我正在尝试访问该对象的字符串并传入该函数但不知道如何操作。此处不允许拆分

var Result = { "win": 1, "loss": 2, "tie": 3 }

function PokerHand(hand) {
}
PokerHand.prototype.compareWith = function(hand){
    // Start your coding here...
    var myHand = this.hand.split(' ');
    var opHand = hand.split(' ');
    if (myHand[0] > opHand[0])
      return Result.win;
    if (this.hand != hand)
      return Result.win;
    return Result.tie;
}
var player = "4S 5H 6H TS AC";
var opponent =  "3S 5H 6H TS AC";
var p = new PokerHand(player);
var o = new PokerHand(opponent);
p.compareWith(o)

1 个答案:

答案 0 :(得分:1)

试试这个。有用。 compareWith()函数中的参数应该是PokerHand,而不是手牌。所以,我只是在使用手的地方做了player.hand。

还有一件事,PokerHand构造函数需要有一个手部属性。

var Result = { "win": 1, "loss": 2, "tie": 3 }

function PokerHand(hand) {
  this.hand = hand;
}
PokerHand.prototype.compareWith = function(pokerHand){
    // Start your coding here...
    var myHand = this.hand.split(' ');
    var opHand = pokerHand.hand.split(' ');
    if (myHand[0] > opHand[0])
      return Result.win;
    if (this.hand != pokerHand.hand)
      return Result.win;
    return Result.tie;
}
var player = "4S 5H 6H TS AC";
var opponent =  "3S 5H 6H TS AC";
var p = new PokerHand(player);
var o = new PokerHand(opponent);
console.log(p.compareWith(o))