English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
As shown below:
//Implement enumeration type, card application function creatEnum(p){ //Constructor var Enumeration = function(){throw 'can not Instantiate Enumerations';}; //Rewrite the prototype and assign it to the variable proto var proto = Enumeration.prototype = { constructor: Enumeration, toString: function(){return this.name;}, valueOf: function(){return this.value;}, toJSON: function(){return this.name;} }; //Add class attributes, methods Enumeration.values = []; for(var n in p){ //Each element of object p is stored separately into a separate object o, and these objects o are stored in the class attribute values array var o = Object.create(proto); //Object o inherits from Enumeration3Enumeration.prototype.valueOf = function(){return this.value ;};*1; //Rewrite the original prototype valueof method o.name = n; o.value = p[n]; Enumeration[n] = o; //Add class attribute name, value is object 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); /*Result: Enumeration object 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 itself inherits from the enumeration object and modifies the valueof method to convert the value to a number for calculation //===Use the function creatEnum() to represent a deck of54张的扑克牌== 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:3Four:4Five:5Six:6Seven: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; }; //Define a standard deck of playing cards function Deck() { var cards = this.cards = []; Card.Suit.foreach(function(s) { //Execute for each suit if(s !=5cards.push(new Card(s, r)); Card.Rank.foreach(function(r) { if (r != 16 ) && r != 17cards.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)); }); } }); } //Shuffle the cards and return the shuffled deck 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; }; //Deal cards and return the array of cards Deck.prototype.deal = function(n){ if(this.cards.length<n) throw 'Out of cards'; return this.cards.splice(this.cards.length-n, n); }; //Start: 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); }
This is the full content of the JavaScript learning notes compiled by the editor for everyone, including the simple implementation of enumeration types and the poker card application. I hope it will be helpful to everyone, and please support the Shouting Tutorial~