如下所示:
//實現枚舉類型,撲克牌應用 function creatEnum(p){ //構造函數 var Enumeration = function(){throw 'can not Instantiate Enumerations';}; //重寫原型並將原型賦值給變量proto var proto = Enumeration.prototype = { constructor:Enumeration, toString:function(){return this.name;}, valueOf:function(){return this.value;}, toJSON:function(){return this.name;} }; //添加類屬性,方法 Enumeration.values = []; for(var n in p){ //將對象p的每個元素都單獨轉存到一個單獨的對象o裡面,並將這些對象o存入類屬性values數組中 var o = Object.create(proto); //對象o繼承了Enumeration的3個實例方法和構造函數 Enumeration.prototype.valueOf = function(){return this.value*1;}; //重寫原型的valueof方法 o.name = n; o.value = p[n]; Enumeration[n] = o; //添加類屬性name,值為對象o Enumeration.values.push(o); } Enumeration.foreach = function (f,c) { for(var i =0;i<this.values.length;i++){ f.call(c,this.values[i]); } }; return Enumeration; } //=== var Coin = creatEnum( {Penny:1,Nickel:5,Dime:10,Quarter:25} ); console.log(Coin); /*結果:枚舉對象Coin { [Function] values: [ { [Number: 10] name: 'Penny', value: 1 }, { [Number: 50] name: 'Nickel', value: 5 }, { [Number: 100] name: 'Dime', value: 10 }, { [Number: 250] name: 'Quarter', value: 25 } ], Penny: { [Number: 10] name: 'Penny', value: 1 }, Nickel: { [Number: 50] name: 'Nickel', value: 5 }, Dime: { [Number: 100] name: 'Dime', value: 10 }, Quarter: { [Number: 250] name: 'Quarter', value: 25 }, foreach: [Function] } */ console.log(Coin.Dime+2); //102 Coin.Dime本身繼承自枚舉對象,繼承並修改了valueof方法用來將value轉化為數字做計算 //===使用函數creatEnum()來表示一副54張的撲克牌== function Card(suit,rank){ this.suit = suit; this.rank = rank; } Card.Suit = creatEnum( {Clubs:1,Diamonds:2,Heates:3,Spades:4,Joker:5} ); Card.Rank = creatEnum( {Three:3,Four:4,Five:5,Six:6, Seven:7,Eight:8,Nine:9,Ten:10, Jack:11,Queen:12,King:13,Ace:14,Two:15,SmallJoker:16,BigJoker:17} ); Card.prototype.toString = function(){ return this.rank.toString() +' of '+this.suit.toString(); }; Card.prototype.compareTo = function(that){ if(this.rank<that.rank) return -1; if(this.rank>that.rank) return 1; return 0; }; Card.orderBySuit = function(a,b){ if(a.suit< b.suit) return -1; if(a.suit> b.suit) return 1; return 0; }; Card.orderByRank = function(a,b){ if(a.rank< b.rank) return -1; if(a.rank> b.rank) return 1; return 0; }; //定義一副標准撲克牌 function Deck(){ var cards = this.cards = []; Card.Suit.foreach(function(s){ //對每個花色執行 if(s!=5) { Card.Rank.foreach(function (r) { if (r != 16 && r != 17) { cards.push(new Card(s, r)); } }); }else{ Card.Rank.foreach(function (r){ if(r == 16) cards.push(new Card(s, r)); if(r == 17) cards.push(new Card(s, r)); }); } }); } //洗牌,並返回洗好的牌 Deck.prototype.shuffle = function(){ var deck = this.cards, len = deck.length; for(var i = len-1;i>0;i--){ var r = Math.floor(Math.random()*(i+1)), temp; temp = deck[i], deck[i] = deck[r], deck[r] = temp; } return this; }; //發牌,並返回牌的數組 Deck.prototype.deal = function(n){ if(this.cards.length<n) throw 'Out of cards'; return this.cards.splice(this.cards.length-n, n); }; //開始: var deck = new Deck(); var deck1 =deck.shuffle(); var n = 17; var hand1 = deck1.deal(n).sort(Card.orderByRank); for(var i = 0;i<n;i++){ var body = document.getElementById('body'); var div = document.createElement('div'); div.style.width = '50px'; div.style.height = '100px'; div.style.border = '1px solid gray'; div.style.float = 'left'; div.innerHTML = hand1[i].suit.name+' '+hand1[i].rank.name; body.appendChild(div); console.log(hand1[i].suit.name+' '+hand1[i].rank.name); }
以上就是小編為大家帶來的JavaScript學習筆記整理_簡單實現枚舉類型,撲克牌應用的全部內容了,希望對大家有所幫助,多多支持~