JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用

如下所示:

//实现枚举类型,扑克牌应用
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;ithat.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 
 

以上就是小编为大家带来的JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用的全部内容了,希望对大家有所帮助,多多支持脚本之家~

你可能感兴趣的:(JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用)