<script type= "Text/javascript" >/** * This function creates a new enumeration type, the argument object represents the name and value of each instance of the class * The return value is a constructor that identifies the new class * Note that this constructor also throws an exception and cannot be used to create a new instance of the type * The returned constructor contains a mapping table of name/value pairs * Includes an array of values, and a foreach () iterator function*/functionEnumeration (namestovalues) {//this virtual constructor-style return value varEnumeration =function(){Throw"Can ' t Instantiate enumeration"}; //enumeration values are inherited from this object varProto = Enumeration.prototype ={constructor:enumeration,//Identity TypeTostring:function(){return This. Name; },//return nameValueOf:function(){return This. Value;},//return valueToJSON:function(){return This. Name; },//Convert to JSON }; Enumeration.values=[];//an array to hold the enumeration object //now create an instance of the new type for(NameinchNamestovalues) {//iterate through each value varE = Object.create (proto);//create a object that represents itE.name = name;//Give it a name .E.value = Namestovalues[name];//give it a valueEnumeration[name] = e;//set it to the property of the constructorEnumeration.values.push (e);//store it in a value array } //A class method used to iterate over an instance of a classEnumeration.foreach =function(f,c) { for(vari = 0; i< This. values.length; i++) F.call (c, This. Values[i]); }; //returns a constructor that represents this new type returnenumeration;}//enumeration Types Use examples:Console.log ("Enum type Use Example:");varE =NewEnumeration ({A:1,b:2,c:3}); Console.log (e); Console.log (e.values); E.foreach (console.log,e) ;//An enumeration type represents a deck of cards:Console.log ("enumeration type represents a deck of cards:");//define a class that already represents a "play card"functionCard (suit, rank) { This. suit = suit;//each card has a suit This. rank = rank;//and Points}//using an enumeration class to define color changes and pointsCard.suit = Enumeration ({clubs:1,diamonds:2,hearts:3,spades:4}); Card.rank= Enumeration ({two:2, three:3,four:4, Five:5,six:6, Seven:7,eight:8,nine:9,ten:10,jack:11, Queen:King:13,ace:14, } ) ;//define the text with a description of the cardCard.prototype.toString =function(){ This. rank.tostring () + This. suit.tostring ();};//Compare the size of two cards in pokerCard.prototype.compareTo =function(that) {if( This. Rank<that.rank)return-1; if( This. Rank > That.rank)return1; return0;};//A function of sorting cards by the rules of playing pokerCard.orderbyrank =function(A, B) {returnA.compareto (b)};//A function of sorting cards by the rules of Bridge playCard.orderbysuit =function(A, b) {if(A.suit < B.suit)return-1; if(A.suit > B.suit)return1; if(A.rank < B.rank)return-1; if(A.rank > B.rank)return1; return0;};//defines a class for representing a standard poker deckfunctionDeck () {varCards = This. Cards =[];//a deck of cards is an array of cards.Card.Suit.foreach (function(s) {//Initialize this arrayCard.Rank.foreach (function(R) {Cards.push (NewCard (s,r)); }); });}//Shuffle the way: re-shuffle and return to wash the cardsDeck.prototype.shuffle =function(){ //iterates through each element of the array, randomly finds the smallest element of the board and swaps it with (the currently traversed element) varDeck = This. Cards,len =deck.length; for(vari = len-1; i>0; i--){ varR = Math.floor (Math.random () * (i+1)), temp;//Random Suetemp = Deck[i],deck[i] = Deck[i], deck[r] = temp;//Exchange } return This;};//method of licensing: Returns an array of cardsDeck.prototype.deal =function(n) {if( This. cards.length < N)Throw"Out of Cards"; return This. Cards.splice ( This. cards.length-n,n);};D Eck.prototype.toString=function(){ vars = ""; for(varXinch This. Cards) {Console.log ( This. Cards[x].suit); Console.log ( This. Cards[x].rank); S+= "{"+ This. cards[x].suit.tostring () + "" + This. cards[x].rank.value+ "}"; } returns;};//create a new deck of cards, shuffle and parallel cardsvarDeck = (NewDeck ()). Shuffle ();varHand = Deck.deal (13). Sort (card.orderbysuit);d eck.tostring (); Console.log (deck.tostring ());//Console.log (hand);//Console.log (hand.name);</script>
JavaScript a simple enumeration type implements poker