English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
como se muestra a continuación:
//Implementar tipo de enumeración, aplicación de cartas function creatEnum(p){ //Constructor var Enumeration = function(){throw 'no se pueden instanciar Enumerations';}; //Reescribe el prototipo y asigna el prototipo a la variable proto var proto = Enumeration.prototype = { constructor: Enumeration, toString: function(){return this.name;}, valueOf: function(){return this.value;}, toJSON: function(){return this.name;} }; //Añade atributos de clase, métodos Enumeration.values = []; for(var n in p){ //Guarda cada elemento del objeto p en un objeto o separado y almacena estos objetos o en el array de atributos de clase values var o = Object.create(proto); //El objeto o hereda de Enumeration3métodos de instancia y constructor de una instancia Enumeration.prototype.valueOf = function(){return this.valor*1;}; //Reescribir el método valueof del prototipo o.nombre = n; o.valor = p[n]; Enumeration[n] = o; //Agregar atributo de clase name, valor del objeto o Enumeration.valores.push(o); } Enumeration.foreach = function (f,c) { for(var i =0;i<this.valores.length;i++{ f.call(c,this.valores[i]); } }; return Enumeration; } //=== var Coin = creatEnum( {Centavo:}1, Nickel:5, Dime:10, Quarter:25}; console.log(Coin); /*Resultado: objeto de enumeración Coin { [Función] valores: [ { [Número: 10)] nombre: 'Centavo', valor: 1 }, { [Número: 50] nombre: 'Nickel', valor: 5 }, { [Número: 100] nombre: 'Dime', valor: 10 }, { [Número: 250] nombre: 'Quarter', valor: 25 } ], Centavo: { [Número: 10)] nombre: 'Centavo', valor: 1 }, Nickel: { [Número: 50] nombre: 'Nickel', valor: 5 }, Dime: { [Número: 100] nombre: 'Dime', valor: 10 }, Quarter: { [Número: 250] nombre: 'Quarter', valor: 25 }, foreach: [Function] } */ console.log(Coin.Dime+2); //102 Coin.Dime hereda de un objeto de enumeración, hereda y modifica el método valueof para convertir el valor en un número para cálculos //===Usar la función creatEnum() para representar un mazo de54Una carta de poker== function Card(suit,rank){ this.suit = suit; this.rank = rank; } Card.Suit = creatEnum( {Tréboles:}1, Diamantes:2, Corazones:3, Espadas:4, Joker:5}; Card.Rank = creatEnum( {Tres:}3, Cuatro:4, Cinco:5, Seis:6, Siete:7, Ocho:8, Nueve:9, Diez:10, Jota:11, Reina:12, Rey:13, As:14, Dos:15, Pequeño Joker:16, Gran Joker:17}; Card.prototype.toString = function() { return this.rank.toString() +de '+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; return 0; }; Card.orderByRank = function(a,b) { if(a.rank< b.rank) return -1; if(a.rank> b.rank) return 1; return 0; }; //Definir un mazo de cartas estándar function Deck() { var cards = this.cards = []; Card.Suit.foreach(function(s) { //Ejecutar para cada palo if(s!=5cards.push(new Card(s, r)); Card.Rank.foreach(function (r) { if (r != 16 ) && r != 17cards.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)); }); } }); } //Barajar, y devolver las cartas mezcladas 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; }; //Distribuir cartas y devolver un array de cartas Deck.prototype.deal = function(n){ if(this.cards.length<n) throw 'Out of cards'; return this.cards.splice(this.cards.length-n, n); }; //Comienzo: 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 sólido gris'; 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); }
Aquí termina la recopilación de apuntes de aprendizaje de JavaScript que he preparado para ustedes: implementación simple del tipo enumerativo, aplicación de cartas, espero que les haya sido útil y que sigan apoyando el tutorial gritando~