JavaScript learning notes sorting_Simple implementation of enumeration types, playing cards application
Updated: September 19, 2016 08:31:56 Submission: jingxian
Below, the editor will bring you a JavaScript learning notes compilation_simple implementation of enumeration types and playing cards. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor
As shown below:
//Implement enumeration type, playing card applicationfunction creatEnum(p){ //Constructor var Enumeration = function(){throw 'can not Instantiate Enumerations';}; //Rewrite the prototype and assign the prototype to the variable proto var proto = = { constructor:Enumeration, toString:function(){return ;}, valueOf:function(){return ;}, toJSON:function(){return ;} }; //Add class attributes, method = []; for(var n in p){ //Save each element of object p to a separate object o, and store these objects o in the class attribute values array var o = (proto); //Object o inherits 3 instance methods and constructors of Enumeration = function(){return *1;}; //Rewrite the valueof method of the prototype = n; = p[n]; Enumeration[n] = o; //Add class attribute name, value is object o (o); } = function (f,c) { for(var i =0;i<;i++){ (c,[i]); } }; return Enumeration; } //=== var Coin = creatEnum( {Penny:1,Nickel:5,Dime:10,Quarter:25} ); (Coin); /*Result: Enumeration object 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] } */ (+2); //102 It inherits from the enumeration object, inherits and modifys the valueof method to convert value into numbers for calculation //===Use the function creatEnum() to represent a deck of 54 playing cards ==function Card(suit,rank){ = suit; = rank; } = creatEnum( {Clubs:1,Diamonds:2,Heates:3,Spades:4,Joker:5} ); = 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} ); = function(){ return () +' of '+(); }; = function(that){ if(<) return -1; if(>) return 1; return 0; }; = function(a,b){ if(< ) return -1; if(> ) return 1; return 0; }; = function(a,b){ if(< ) return -1; if(> ) return 1; return 0; }; //Define a standard deck of playing cardsfunction Deck(){ var cards = = []; (function(s){ //Execute each suit if(s!=5) { (function (r) { if (r != 16 && r != 17) { (new Card(s, r)); } }); }else{ (function (r){ if(r == 16) (new Card(s, r)); if(r == 17) (new Card(s, r)); }); } }); } //Shut the cards and return the shuffled cards = function(){ var deck = , len = ; for(var i = len-1;i>0;i--){ var r = (()*(i+1)), temp; temp = deck[i], deck[i] = deck[r], deck[r] = temp; } return this; }; //Discount the card and return the card array = function(n){ if(<n) throw 'Out of cards'; return (-n, n); }; //start:var deck = new Deck(); var deck1 =(); var n = 17; var hand1 = (n).sort(); for(var i = 0;i<n;i++){ var body = ('body'); var div = ('div'); = '50px'; = '100px'; = '1px solid gray'; = 'left'; = hand1[i].+' '+hand1[i].; (div); (hand1[i].+' '+hand1[i].); }
Related Articles
Introduction to the use of italics() method in JavaScript
This article mainly introduces the use of italics() method in JavaScript, which is the basic knowledge in JS introduction. Friends who need it can refer to it.2015-06-06How to optimize JavaScript reference code using RequireJS
This article mainly introduces the method of optimizing JavaScript reference code using RequireJS. RequireJS is a popular JS library. Friends who need it can refer to it.2015-07-07Brief analysis of the working principle of JavaScript mapreduce
MapReduce is a programming model used for job scheduling and is also a related implementation of an algorithm model for processing and generating super-large data sets. This article will introduce in detail the working principle of JavaScript mapreduce. Friends who need it can refer to it.2012-11-11Detailed explanation of the support of the Unicode character set in JavaScript language
This article mainly introduces the detailed explanation of the JavaScript language's support for Unicode character sets. Friends who need it can refer to it.2014-12-12A brief discussion on javascript callback function
Callback functions are concepts derived from a programming paradigm called functional programming. Simply put, functional programming is about using functions as variables. Functional programming used to be - and even now, still not widely used - it used to be regarded as the secret skill of franchise-trained, master-level programmers.2014-12-12Clever talk about the usage of JavaScript arrays
Below, the editor will bring you an old article about the usage of JavaScript arrays. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor2016-06-06Summary of the use of analysis
This article provides a detailed summary and introduction to the usage method. If you need it, please refer to it.2013-06-06JS get url link string
Sometimes we need to get the URL of the current web page for our convenience. Generally, we get it through2013-12-12Javascript Math object detailed explanation
This article mainly introduces the usage of Math objects in JavaScript, and the explanation is very detailed. I hope it can be used as a reference for you.2016-06-06Rookie JavaScript Basic Information Compilation 3 Regularity
Use of js regular expressions (RegExp object).2010-12-12