《javascript权威指南》第9章 例9-8源码
//创建一个新的枚举类型
//不能使用它来创建该类型的新实例
function Enumeration(nameToValues){
var Enumeration = function(){throw "Can't Instantiate Enumerations";}; var proto = Enumeration.prototype = {
constructor: Enumeration,
toString: function(){return this.name;},
valueOf: function(){return this.value;},
toJSON: function(){return this.name;}
}; Enumeration.values = []; for(name in nameToValues){
var e = Object.create(proto);
e.name = name;
e.value = nameToValues[name];
Enumeration[name] = e;
Enumeration.values.push(e);
} Enumeration.foreach = function(f, c){
for(var i = 0; i < this.values.length; i++) f.call(c, this.values[i]);
}; return Enumeration;
} function Card(suit, rank){
this.suit = suit;
this.rank = rank;
} 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}); 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;
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){
Card.Rank.foreach(function(r){
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 < n) throw "Out of cards";
return this.cards.splice(this.cards.length - n, n);
} var deck = (new Deck()).shuffle();
var hand = deck.deal(13).sort(Card.orderBySuit); console.log(hand);
《javascript权威指南》第9章 例9-8源码的更多相关文章
- javascript权威指南第11章 DOM扩展
//javascript 权威指南 第三版 第11章 DOM扩展 //取得body元素 var body = document.querySelector("body"); //取 ...
- 【笔记】javascript权威指南-第六章-对象
对象 //本书是指:javascript权威指南 //以下内容摘记时间为:2013.7.28 对象的定义: 1.对象是一种复合值:将很多值(原始值或者对象)聚合在一起,可以通过名字访问这些值. ...
- 【笔记】javascript权威指南-第三章-类型,值和变量
javascript中的原始类型和对象类型(基本类型和引用类型) //本书是指:javascript权威指南 //以下内容摘记时间为:2013.7.27 计算机程序运行时需要对值(value ...
- javascript权威指南第16章 HTML5脚本编程
<!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...
- JavaScript权威指南--第3章 类型、值和变量
在编程语言中,能够表示并操作的值(value)的类型称作数据类型(type).使用变量来储存值.JavaScript中数据类型有两种:原始类型(primitive type/基本数据类型)和对象类型( ...
- JavaScript权威指南第02章 词法结构
词法结构 2.1字符集 JavaScript 是Unicode字符集编写,差点儿支持地球上全部的语言. 2.1.1区分大写和小写 javascript是区分大写和小写的语言. 2.1.2 空格.换行符 ...
- JavaScript权威指南第01章 JavaScript 概述
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangbin10025/article/details/27951767 JavaScript 概 ...
- JavaScript权威指南第03章 类型、值和变量(1)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/huangbin10025/article/details/27953481 类型.值和变量 数据类型 ...
- javascript权威指南第22章高级技巧
HTML <!DOCTYPE html> <html> <head> </head> <body> <div style=" ...
- javascript权威指南第20章 JSON
//20.1 语法 //JAVASCRIPT 是对JSON数据支持的. //JSON 可以申明三种类型的值 简单值("hello world") 对象({"name&qu ...
随机推荐
- C51系列RAM寄存器表
特殊功能寄存器地址表 SFR 符号 字节 地址 位地址和位名称 D7 D6 D5 D4 D3 D2 D1 D0 P0口 P0 80H P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0. ...
- 【转】ActionBar的基本用法
原文网址:http://irtutsk.iteye.com/blog/2117707 ActionBar的组成: [1]AppIcon:标题区,显示应用程序图标和标题,也可以自定义. [2]ViewC ...
- Windows服务器Pyton辅助运维--03.安装Visual Studio 的 Python 开发插件 PTVS
PTVS (Python Tools for Visual Studio) http://pytools.codeplex.com/ 当前版本:2.1 RC PTVS (Python Tools fo ...
- BIND9源码学习笔记1---gdb调试篇
学习bind9源码之前,首先要知道如何用gdb来调试bind.BIND9的源码我是先看代码弄懂它的架构,像什么event-drive,epoll等, 再去看它的业务流程.看业务流程的时候要追踪它的数据 ...
- Kafka小记
kafka简介 kafka是由LinkedIn开发,主要是用来处理Linkedin的大面积活跃数据流处理(activity stream). 此类的数据经常用来反映网站的一些有用的信息,比如PV,页 ...
- Java_XML操作
xml的写: code: import java.io.File; import java.io.StringWriter; import javax.xml.parsers.DocumentBuil ...
- facebook分享遇到的错误解决方法
*** Terminating app due to uncaught exception 'InvalidOperationException', reason: ''App ID not foun ...
- zeptoJS:如何像jQuery一样,让滚动变得优雅?
利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果: $(function() { $('#top').click( function (e) { $('html, bod ...
- [转]RecyclerView初探
原文地址:http://www.grokkingandroid.com/first-glance-androids-recyclerview/ RecyclerView是去年谷歌I/O大会上随Andr ...
- JavaScript中的面向对象的讨论(转)
前言 今天,WEB2.0时代的到来,给了JavaScript又一次大展身手的机会.Web2.0借助JavaScript技术,使得客户端的Web体验更加丰富多彩,同时JavaScript面对的问题域也变 ...