在Javascript中处理卡片

时间:2013-06-04 04:47:23

标签: javascript

我正在尝试创建一个函数来在javascript中从卡片中处理卡片但是当我在交易函数中为将来的对象属性创建参数时,它将该属性返回为undefined。为什么会这样?

//Creates the deck
var deck = Math.ceil(Math.random() * 52) + 1;
var Ace = 1;
var Face = 10;
if (deck % 13 == 1) {
deck = Ace;
} else if (deck % 13 === 11 || deck % 13 === 12 || deck % 13 === 0) {
    deck = Face;
} else if (deck % 13 === 2) {
    deck = 2;
} else if (deck % 13 === 3) {
    deck = 3;
} else if (deck % 13 === 4) {
    deck = 4;
} else if (deck % 13 === 5) {
    deck = 5;
} else if (deck % 13 === 6) {
    deck = 6;
} else if (deck % 13 === 7) {
    deck = 7;
} else if (deck % 13 === 8) {
    deck = 8;
} else if (deck % 13 === 9) {
    deck = 9;
} else {
    deck = 10;
}
//Creates the player as an object
function Player(pcard1) {
    pcard1 = pcard1;
}
//Deals a random card out of the deck
var deal = function (pob) {
    pob = deck;
};
deal(Player.pcard1);
alert(Player.pcard1);

2 个答案:

答案 0 :(得分:0)

经过一些评论,一些初步说明。我猜你错过了一些关于Javascript工作的基础:

  1. 玩家是一个功能,这是一个很好的起点。但是,您然后尝试访问它的成员。只有在从函数中实例化一个新对象,并将其用作构造函数之后,才可能这样做:

    var player = new Player(card_value);
    player.pcard1; // this is the public member of a Player instance
    
  2. 交易功能应该返回一个值,或者遵循上述指南;

  3. 目前还不清楚(虽然两者都是正确的)为什么你使用“函数FunctionName”然后使用“var FunctionName = function”方法。

  4. 那就是说,你可以完全跳过long if / else块,用更结构化的方法替换它:

    var Ace = 1;
    var Face = 10;
    var symbols = [Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Face, Face, Face];
    var deal = function() {
        var card = Math.random() * 51;
        return symbols[Math.floor(card % 12)];
    }
    
    var Player = function(card_value) {
        this.card = card_value;
    }
    
    var card_value = deal();
    var player = new Player(card_value);
    alert(player.card);
    

    然后您可以通过简单的方式为播放器分配一张新卡片:

    player.card = deal();
    

    请记住,你可以将任何你喜欢的成员附加到玩家身上:

    player.name = "Rambo";
    

答案 1 :(得分:0)

通过传递Player.pcard1,您只会将变量的当前值传递给bob参数,无论您对bob执行什么操作都不适用于Player.pcard1

您应该为不同的参数编写不同的函数。

//Creates the player as an object
function Player(pcard1) {
    Player.pcard1 = pcard1;
}
//Deals a random card out of the deck
var deal = function () {
    Player.pcard1 = deck;
};
var deal2=function(){
    Player.id="hello";   
}
deal();
deal2();
alert(Player.pcard1);
alert(Player.id);

(或)

写函数返回值:

function getDeckValue(){
        return deck;
 }
 function getID(){
        return "1234";
  }
 Player.pcard1=getDeckValue();
 Player.id=getID();

(OR)

使用@sixFingers指出的javascrit的灵活概念,定义一个类实例化它写成员函数等。     通过这种方式,您只需访问Player.variable等..

jquery开发人员在javascript上的优秀tutrial(我的意思是)Advanced Javascript。它涉及你想要的所有oop概念,并讨论了你在w3schools上找不到的javascript的许多功能。

编辑:第二个例子的工作代码

    //Creates the deck
var deck = Math.ceil(Math.random() * 52) + 1;
var Ace = 1;
var Face = 10;
if (deck % 13 == 1) {
    deck = Ace;
} else if (deck % 13 === 11 || deck % 13 === 12 || deck % 13 === 0) {
    deck = Face;
} else if (deck % 13 === 2) {
    deck = 2;
} else if (deck % 13 === 3) {
    deck = 3;
} else if (deck % 13 === 4) {
    deck = 4;
} else if (deck % 13 === 5) {
    deck = 5;
} else if (deck % 13 === 6) {
    deck = 6;
} else if (deck % 13 === 7) {
    deck = 7;
} else if (deck % 13 === 8) {
    deck = 8;
} else if (deck % 13 === 9) {
    deck = 9;
} else {
    deck = 10;
}
//Creates the player as an object
function Player(pcard1){
   Player.pcard1=pcard1;   
}
function getDeckValue(){
        return deck;
 }
 function getID(){
        return "1234";
  }
 Player.pcard1=getDeckValue();
 Player.id=getID();

alert(Player.pcard1);
alert(Player.id);