• 《javascript权威指南》第9章 例9-8源码


     1 //创建一个新的枚举类型
     2 //不能使用它来创建该类型的新实例
     3 function Enumeration(nameToValues){
     4     var Enumeration = function(){throw "Can't Instantiate Enumerations";};
     5 
     6     var proto = Enumeration.prototype = {
     7         constructor: Enumeration,
     8         toString: function(){return this.name;},
     9         valueOf: function(){return this.value;},
    10         toJSON: function(){return this.name;}
    11     };
    12 
    13     Enumeration.values = [];
    14 
    15     for(name in nameToValues){
    16         var e = Object.create(proto);
    17         e.name = name;
    18         e.value = nameToValues[name];
    19         Enumeration[name] = e;
    20         Enumeration.values.push(e);
    21     }
    22 
    23     Enumeration.foreach = function(f, c){
    24         for(var i = 0; i < this.values.length; i++) f.call(c, this.values[i]);
    25     };
    26 
    27     return Enumeration;
    28 }
    29 
    30 function Card(suit, rank){
    31     this.suit = suit;
    32     this.rank = rank;
    33 }
    34 
    35 Card.Suit = Enumeration({Clubs:1, Diamonds:2, Hearts:3, Spades: 4});
    36 Card.Rank = Enumeration({Two:2,Three:3,Four:4,Five:5,Six:6,
    37                          Seven:7,Eight:8,Nine:9,Ten:10,
    38                          Jack:11,Queen:12,King:13,Ace:14});
    39 
    40 Card.prototype.toString = function(){
    41         return this.rank.toString() + " of " + this.suit.toString();
    42 };
    43 
    44 Card.prototype.compareTo = function(that){
    45         if(this.rank < that.rank) return -1;
    46         if(this.rank > that.rank) return 1;
    47         return 0;
    48 };
    49 
    50 Card.orderBySuit = function(a, b){
    51     if(a.suit < b.suit) return -1;
    52     if(a.suit > b.suit) return 1;
    53     if(a.rank < b.rank) return -1;
    54     if(a.rank > b.rank) return 1;
    55     return 0;
    56 }
    57 
    58 function Deck(){
    59     var cards = this.cards = [];
    60     Card.Suit.foreach(function(s){
    61                         Card.Rank.foreach(function(r){
    62                                              cards.push(new Card(s, r));
    63                                          });
    64                      });
    65 }
    66 
    67 Deck.prototype.shuffle = function(){
    68     var deck = this.cards, len = deck.length;
    69     for(var i = len - 1; i > 0; i--){
    70         var r = Math.floor(Math.random() * (i + 1)),
    71             temp;
    72         temp = deck[i], deck[i] = deck[r], deck[r] = temp;
    73     }
    74 
    75     return this;
    76 }
    77 
    78 Deck.prototype.deal = function(n){
    79     if(this.cards.length < n) throw "Out of cards";
    80     return this.cards.splice(this.cards.length - n, n);
    81 }
    82 
    83 var deck = (new Deck()).shuffle();
    84 var hand = deck.deal(13).sort(Card.orderBySuit);
    85 
    86 console.log(hand);
  • 相关阅读:
    今日总结
    今日总结
    团队绩效1
    本周总结
    团队冲刺阶段10
    团队冲刺阶段9
    团队冲刺阶段8
    promise手写自定义封装异步任务回调的执行
    Vue中this.$options.data()和this.$data知多少?
    手写Promise自定义封装 then 函数
  • 原文地址:https://www.cnblogs.com/charles-dxb/p/5121468.html
Copyright © 2020-2023  润新知