JavaScript learning notes _ simple implementation of enumeration types, playing card applications, javascript learning notes

Source: Internet
Author: User

JavaScript learning notes _ simple implementation of enumeration types, playing card applications, javascript learning notes

As follows:

// Implement the Enumeration type. the playing card application function creatEnum (p) {// constructor var Enumeration = function () {throw 'can not Instantiate Enumerations ';}; // rewrite the prototype and assign the prototype to the variable proto var proto = Enumeration. prototype = {constructor: Enumeration, toString: function () {return this. name ;}, valueOf: function () {return this. value;}, toJSON: function () {return this. name ;}}; // Add class property, method Enumeration. values = []; for (var n in p) {// transfers each element of object p to a separate object o And store these objects o into the class property values array var o = Object. create (proto); // object o inherits the three instance methods and constructor Enumeration of Enumeration. prototype. valueOf = function () {return this. value * 1 ;}; // rewrite the valueof method o of the prototype. name = n; o. value = p [n]; Enumeration [n] = o; // Add the class attribute name with the value of o Enumeration. values. push (o);} Enumeration. foreach = function (f, c) {for (var I = 0; I <this. values. length; I ++) {f. call (c, this. values [I]) ;}}; return Enumeration ;}// = v Ar Coin = creatEnum ({Penny: 1, Nickel: 5, Dime: 10, Quarter: 25}); console. log (Coin);/* result: the enumerated 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]} */console. log (Coin. dime + 2); // 102 Coin. dime itself inherits from the enumeration object, inherits and modifies the valueof method to convert value into numbers for calculation // === use the creatEnum () function () to represent a pair of 54 cards = function Card (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 () {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 (. suit <B. suit) retu Rn-1; if (. suit> B. suit) return 1; return 0;}; Card. orderByRank = function (a, B) {if (. rank <B. rank) return-1; if (. rank> B. rank) return 1; return 0 ;}; // define a pair of standard playing card functions Deck () {var cards = this. cards = []; Card. suit. foreach (function (s) {// execute 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) ;}}}) ;}// shuffles and returns the washed Card. prototype. shuffle = function () {var deck = this. cards, len = deck. length; for (vari = 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;}; // returns the card array Deck. prototype. deal = function (n) {if (this. cards. length <n) throw 'out of cards '; return this. cards. splice (this. cards. length-n, n) ;}; // start: var deck = new Deck (); var deck1 = deck. shuffle (); var n = 17; var hand1 = deck1.deal (n ). sort (Card. orderByRank); for (var I = 0; I <n; I ++) {var body = document. getElementById ('body'); var div = document. createElement ('div '); div. style. width = '50px '; div. style. height = '100px '; div. style. border = '1px solid gray '; 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 );}

The above is the summary of JavaScript learning notes for everyone _ simple implementation of Enumeration type, all the content of the poker application, hope to help everyone, a lot of support for the house ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.