JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用
如下所示:
//实现枚举类型,扑克牌应用 functioncreatEnum(p){ //构造函数 varEnumeration=function(){throw'cannotInstantiateEnumerations';}; //重写原型并将原型赋值给变量proto varproto=Enumeration.prototype={ constructor:Enumeration, toString:function(){returnthis.name;}, valueOf:function(){returnthis.value;}, toJSON:function(){returnthis.name;} }; //添加类属性,方法 Enumeration.values=[]; for(varninp){//将对象p的每个元素都单独转存到一个单独的对象o里面,并将这些对象o存入类属性values数组中 varo=Object.create(proto);//对象o继承了Enumeration的3个实例方法和构造函数 Enumeration.prototype.valueOf=function(){returnthis.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(vari=0;i<this.values.length;i++){ f.call(c,this.values[i]); } }; returnEnumeration; } //=== varCoin=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);//102Coin.Dime本身继承自枚举对象,继承并修改了valueof方法用来将value转化为数字做计算 //===使用函数creatEnum()来表示一副54张的扑克牌== functionCard(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(){ returnthis.rank.toString()+'of'+this.suit.toString(); }; Card.prototype.compareTo=function(that){ if(this.rank<that.rank)return-1; if(this.rank>that.rank)return1; return0; }; Card.orderBySuit=function(a,b){ if(a.suit<b.suit)return-1; if(a.suit>b.suit)return1; return0; }; Card.orderByRank=function(a,b){ if(a.rank<b.rank)return-1; if(a.rank>b.rank)return1; return0; }; //定义一副标准扑克牌 functionDeck(){ varcards=this.cards=[]; Card.Suit.foreach(function(s){//对每个花色执行 if(s!=5){ Card.Rank.foreach(function(r){ if(r!=16&&r!=17){ cards.push(newCard(s,r)); } }); }else{ Card.Rank.foreach(function(r){ if(r==16)cards.push(newCard(s,r)); if(r==17)cards.push(newCard(s,r)); }); } }); } //洗牌,并返回洗好的牌 Deck.prototype.shuffle=function(){ vardeck=this.cards,len=deck.length; for(vari=len-1;i>0;i--){ varr=Math.floor(Math.random()*(i+1)),temp; temp=deck[i],deck[i]=deck[r],deck[r]=temp; } returnthis; }; //发牌,并返回牌的数组 Deck.prototype.deal=function(n){ if(this.cards.length<n)throw'Outofcards'; returnthis.cards.splice(this.cards.length-n,n); }; //开始: vardeck=newDeck(); vardeck1=deck.shuffle(); varn=17; varhand1=deck1.deal(n).sort(Card.orderByRank); for(vari=0;i<n;i++){ varbody=document.getElementById('body'); vardiv=document.createElement('div'); div.style.width='50px'; div.style.height='100px'; div.style.border='1pxsolidgray'; 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学习笔记整理_简单实现枚举类型,扑克牌应用的全部内容了,希望对大家有所帮助,多多支持毛票票~