javaScript 权威指南读书笔记(类和模块)
对于js模拟java实现枚举,阅读了犀牛书上的例子,稍微做了下改变
/**
*This method is mainly to create an object whose prototype is p
*@param {Object} p
*@return {Object}
**/
function inherit(p){ if(p==null)throw new TypeError; if(Object.create)return Object.create(p); var t=typeof p; if(t!=="function"||t!=="object")throw new TypeError; function f(){}; f.prototype=p; return new f(); } /**
*Implement the enumeration
*@param {JSON} nameToValues
*@return {Function}
**/ function enumeration(nameToValues){ var enuFun=function(){throw "Can't instantiate Enumeration";} var proto=enuFun.prototype={ constructor:enuFun, toString:function(){return this.name;}, valueOf:function(){return this.value;}, toJSON:function(){return this.name} }; enuFun.values=[]; for(name in nameToValues){ var e=inherit(proto); e.name=name; e.value=nameToValues[name]; enuFun[name]=e; enuFun.values.push(e); } enuFun.foreach=function(f,c){ for(var i=0;i<this.values.length;i++)f.call(c,this.values[i]); } return enuFun; }
/**
*The constructor of Card
**/ function Card(suit,rank){ this.suit=suit; this.rank=rank; } //create enumeration for Card Card.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:12,King:13,Ace:14});
//Attach some methods to Card's prototype.It's one of the way implementing inheritance
/**
* Return the description of one card.
**/ 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.orderByRank=function(a,b){ return a.compareTo(b); } Card.oderBySuit=function(a,b){ if(a.suit<b.suit) return -1; if(a.suit>b.suit)return 1; if(a.rank<b.rank)return -1; if(a.rank>b.rank)return 1; } function Deck(){ var cards=this.cards=[]; Card.SUIT.foreach(function(suit){ Card.RANK.foreach(function(rank){ cards.push(new Card(suit,rank)); }); }); } 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(num){ var deck=this.cards,len=deck.length; if(num>len) throw "Out of cards!"; return deck.splice(len-num,num); } var deck=new Deck().shuffle(); function distributeCards(n){ if(n>10)throw "The number of game players can't exceed 10"; var total=deck.cards.length,per=total/n,remain=total%n; var i=0,ordinalArr=['First','Second','Third','Fourth','Fifth','Sixth','Seventh','Eighth','Nineth','Tenth']; while(i<n){ console.log("------------------------------------The "+ordinalArr[i]+" person's Cards------------------------") deck.deal(per).sort(Card.sortBySuit).forEach(function(card){ console.log(card.toString()); }); i++; } console.log("------------------------------------There are "+remain+" cards remained----------------------"); deck.cards.forEach(function(card){ console.log(card.toString()); }); } distributeCards(5);
posted on 2014-03-18 16:32 Young_Junior_Man 阅读(131) 评论(0) 收藏 举报
浙公网安备 33010602011771号