我可以使用this.something而不定义它吗?

时间:2017-05-14 18:04:14

标签: javascript switch-statement probability

https://jsfiddle.net/b3nyzp9n/
当调用probabilityAPI.card()时,它返回' of'
看起来像这个.apicardrankk根本不存在。
因此,当我定义apicardrankvar和apicardsuitvar时,它返回" undefined of undefined"。 我试图让它说出一个名片,例如" 3 of Diamonds"

var probabilityAPI = {
    coin: function() {
    if(Math.random() >= 0.5) {
    return "Heads";
    } else if(Math.random() > 0.5){
    return "Tails";
    }},
dice: function() {
  return Math.floor(Math.random() * (Math.floor(7) - Math.ceil(1))) + Math.ceil(1);
},
spinner: function() {
  return Math.floor(Math.random() * (Math.floor(9) - Math.ceil(0))) + Math.ceil(1);
},
apimarblevar: Math.floor(Math.random() * (Math.floor(6) - Math.ceil(1))) + Math.ceil(1),
marble: function(apimarblevar) {
    switch(this.apimarblevar)
{
    case 1:
    return "A"
    break;
    case 2:
    return "B"
    break;
    case 3:
    return "C"
    break;
    case 4:
    return "D"
    break;
    case 5:
    return "E"
    break;
}
},
apidreidelvar: Math.floor(Math.random() * (Math.floor(5) - Math.ceil(1))) + Math.ceil(1),
dreidel: function() {
    switch(this.apidreidelvar) {
        case 1:
        return "Nun"
        break;
        case 2:
        return "Gimel"
        break;
        case 3:
        return "He"
        break;
        case 4:
        return "Shin"
        break;     
    }
},
apicardsuitvar: "",
apicardrankvar: "",
apicardrankrandom: Math.floor(Math.random() * (Math.floor(14) - Math.ceil(1))) + Math.ceil(1),
apicardsuit: function(){
    switch(this.apidreidelvar) {
        case 1:
        this.apicardsuitvar = "Clubs"
        break;
        case 2:
        this.apicardsuitvar = "Diamonds"
        break;
        case 3:
        this.apicardsuitvar =  "Hearts"
        break;
        case 4:
        this.apicardsuitvar = "Spades"
        break;
    }
},
apicardrank: function() {
    switch(this.apicardrankrandom) {
        case 1:
        this.apicardrankvar = "Ace"
        break;
        case 2:
        this.apicardrankvar = "2"
        break;
        case 3:
        this.apicardrankvar = "3"
        break;
        case 4:
        this.apicardrankvar = "4"
        break;
        case 5:
        this.apicardrankvar = "5"
        break;
        case 6:
        this.apicardrankvar = "6"
        break;
        case 7:
        this.apicardrankvar = "7"
        break;
        case 8:
        this.apicardrankvar = "8"
        break;
        case 9:
        this.apicardrankvar = "9"
        break;
        case 10:
        this.apicardrankvar = "10"
        break;
        case 11:
        this.apicardrankvar = "Jack"
        break;
        case 12:
        this.apicardrankvar = "Queen"
        break;
        case 13:
        this.apicardrankvar = "King"
        break;
    }
},
card: function() {
    return this.apicardrankvar + " of " + this.apicardsuitvar
}
};
probabilityAPI.card();

0 个答案:

没有答案