Last active
August 25, 2020 06:56
-
-
Save konami12/334d521826f3e358d895b2f263d5c6ac to your computer and use it in GitHub Desktop.
Principio de Abstracciòn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Como primer ejemplo los datos se conseguiran de un | |
"JSON" el cual funciona como una Base de Datos. | |
*/ | |
import Data from "./infoPokemons.json"; | |
class Pokedex { | |
/* | |
Se crea el método para conseguir la información de un | |
"Pokémon". | |
*/ | |
getInfo(name) { | |
// buscamos la data referente al Pokémon | |
const DATA = Data[name] || null; | |
// Decidimos que mensaje mostrar | |
const MESSAGE = DATA || `El pokémon ${name} no existe`; | |
// Pintamos el mensaje | |
console.log(MESSAGE); | |
} | |
} | |
// Creamos la instancia de la clase "Pokedex" | |
const POKEDEX = new Pokedex(); | |
// Solicitamos la data de charmander | |
POKEDEX.getInfo("charmander"); | |
// Salida | |
// { name: "Charmander", type: "Fire", evolutions: ["Charmeleon", "Chrarizar"]}; | |
//==============================================================// | |
/* | |
Utilizando el código anterior realizaremos un cambio con el | |
que los datos se conseguirán desde un "API" pero al implementar | |
estos cambios no tenemos que afectar la forma en la que se esta | |
interactuando con la clase Pokedex 🤯. | |
*/ | |
class Pokedex { | |
/* | |
Se utiliza el mismo método getInfo se recibe los mismos parametros | |
pero su logica interna cambio. | |
*/ | |
getInfo(name) { | |
// Se realizan cambios para conseguir la data desde la API. | |
fetch(`https://workshop-mongo.herokuapp.com/pokemon/name/${name}`) | |
.then((data) => data.json()) | |
.then((data) => { | |
// buscamos la data referente al Pokémon | |
const [DATA = null] = Data; | |
// Decidimos que mensaje mostrar | |
const MESSAGE = DATA || `El pokémon ${name} no existe`; | |
// Pintamos el mensaje | |
console.log(MESSAGE); | |
}); | |
} | |
} | |
// Creamos la instancia de la clase "Pokedex" | |
const POKEDEX = new Pokedex(); | |
// Solicitamos la data de charmander | |
POKEDEX.getInfo("charmander"); | |
// Salida | |
// { name: "Charmander", type: "Fire", evolutions: ["Charmeleon", "Chrarizar"]}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment