Last active
November 22, 2020 21:18
-
-
Save Myrrel/051d1ab4bc775ca65a66014b68c4f31c to your computer and use it in GitHub Desktop.
ecmascript
This file contains hidden or 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
| // es6 ---- junio 2015 | |
| /** | |
| * -----------------------------------------------------------Default Params | |
| */ | |
| function newFunction(name, age, country) { | |
| var name = name || 'Oscar'; | |
| var age = age || 32; | |
| var country = country || 'MX'; | |
| console.log(name, age, country); | |
| } | |
| function newFunctionES6(name = 'Oscar', age = 32, country = 'MX') { | |
| console.log(name, age, country); | |
| } | |
| newFunctionES6(); | |
| newFunctionES6('Ricardo', 23, 'Colombia'); | |
| /** | |
| * --------------------------------------Concatenation - Template Literals | |
| */ | |
| let hello = 'Hello'; | |
| let world = 'World'; | |
| let epicPhrase = hello + ' ' + world + '!'; | |
| // es6 | |
| let epicPhraseES6 = `${hello} ${world}!`; | |
| console.log(epicPhrase); | |
| console.log(epicPhraseES6); | |
| /** | |
| * ----------------------------------------Classes | |
| */ | |
| class Calculator { | |
| constructor() { | |
| this.valueA = 0; | |
| this.valueB = 0; | |
| } | |
| sum(valueA, valueB) { | |
| this.valueA = valueA; | |
| this.valueB = valueB; | |
| return this.valueA + this.valueB; | |
| } | |
| } | |
| const calc = new Calculator(); | |
| console.log('Calc Result -> ', calc.sum(2, 3)); | |
| /** | |
| * ---------------------------------------------Modules | |
| */ | |
| const hello = () => { | |
| return 'Hello World!'; | |
| }; | |
| export default hello; | |
| import { saludar } from './mi-module.js'; // como se importa con nombre puedo usarla directamente, sino debiera hacer hola.saludar() | |
| import hola from './mi-module.js'; // aquí debo hacer hola.saludar() | |
| console.log('Hello Module -> ', hello()); | |
| --- | |
| // otra opcion de exportar sería | |
| export const saludar = () => 'Hello World!'; // y debo usarla import {saludar} from './mi-modulo.js' | |
| //----------- | |
| /** | |
| * ---------------------------------------------Generators | |
| */ | |
| function* helloWorld() { | |
| if (true) { | |
| yield 'Hello, '; | |
| } | |
| if (true) { | |
| yield 'World!'; | |
| } | |
| } | |
| const generatorHello = helloWorld(); | |
| console.log('generatorHello first call -> ', generatorHello.next().value); | |
| console.log('generatorHello second call -> ', generatorHello.next().value); | |
| console.log('generatorHello third call -> ', generatorHello.next().value); | |
| // es7 ---- junio 2016 | |
| /* | |
| ---------------------------------------Includes | |
| */ | |
| let numbers = [1, 2, 3, 7, 8]; | |
| const VALUE = 7; | |
| if (numbers.includes(VALUE)) { | |
| console.log(`Sí se encuentra el valor ${VALUE}`); | |
| } else { | |
| console.log(`No se encuentra el valor ${VALUE}`); | |
| } | |
| /* | |
| -----------------------------------------------Pow | |
| */ | |
| let base = 4; | |
| let exponent = 4; | |
| let result = base ** exponent; | |
| console.log(`Result -> ${result}`); | |
| // es8 ---- junio 2017 | |
| //Object entries devuelve los valores de una matriz. | |
| const data ={ | |
| front:'Alej', | |
| back: 'Rel' | |
| }; | |
| //Tranformar este objeto en una matriz. | |
| const entries = Object.entries(data); | |
| console.log(entries); | |
| [ | |
| ['front','Alej'], | |
| ['back','Rel'] | |
| ] | |
| //Objetc Values: Me devuelve los valores de un objeto a un arreglo. | |
| const data= { | |
| front:'Alej', | |
| back: 'Rel' | |
| } | |
| const values = Object.values(data); | |
| console.log(values); | |
| ['Alej','Rel'] | |
| // Padding: nos permite añadir cadenas vacías a string, pudiendo modificar la cadena string como tal. | |
| //Podría servir del lado del front , para mostrar una estructura de elementos. | |
| const string ='hello'; | |
| // el primer parametro es la longitud total de la cadena | |
| console.log(string.padStart(7,'hi')) // se añade al inicio la palabra 'hi' 'hihello' | |
| console.log(string.padEnd(12,'hi')) // Se añade al final la palabra 'hi' 'hello' | |
| //Trailing comas, nos permite asignar elementos al objeto mediante comas. | |
| const data= { | |
| front:'Alej', // Puede existir | |
| back: 'Rel' | |
| } | |
| //=> async await | |
| const helloWorld = () => { | |
| return new Promise((resolve, reject)=>{ | |
| (false) | |
| ? setTimeout(()=> resolve('Hello Juan Carlos'), 3000) | |
| : reject(new Error('Test Error')) | |
| }); | |
| } | |
| const helloAsync = async () => { | |
| const hello = await helloWorld(); | |
| console.log(hello); | |
| } | |
| helloAsync() | |
| // asi se ejecuta correctamente una promesa | |
| const anotherFunction = async () => { | |
| try { | |
| const hello = await helloWorld() | |
| console.log(hello); | |
| } catch (error) { | |
| console.log(error); | |
| } | |
| } | |
| anotherFunction() | |
| // es9---- | |
| /** | |
| * Spread Operator | |
| */ | |
| const obj = { | |
| name: 'Oscar', | |
| age: 32, | |
| country: 'MX' | |
| }; | |
| let { name, ...addInfo } = obj; | |
| console.log(`name: ${name}`); | |
| console.log(`additional information: `, addInfo); | |
| let { country, ...nameAndAge } = obj; | |
| console.log(`name and age: `, nameAndAge); | |
| /** | |
| * Porpagation Properties | |
| */ | |
| const person = { | |
| name: 'Oscar', | |
| age: 32 | |
| }; | |
| const personInformation = { | |
| ...person, | |
| country: 'MX' | |
| }; | |
| console.log(`personInformation: `, personInformation); | |
| /** | |
| * Promise Finally | |
| */ | |
| const helloWorld = () => { | |
| return new Promise((resolve, reject) => { | |
| true | |
| ? setTimeout(() => { | |
| resolve('Hello World!'); | |
| }, 3000) | |
| : reject(new Error('Test Error')); | |
| }); | |
| }; | |
| helloWorld() | |
| .then(result => console.log('result -> ', result)) | |
| .catch(err => console.log('err -> ', err)) | |
| .finally(() => console.log('finalizó')); | |
| /** | |
| * Regex | |
| */ | |
| const regexData = /([0-9]{4})-([0-9]{2})-([0-9]{2})/; | |
| const match = regexData.exec('2018-04-28'); | |
| const year = match[1]; | |
| const month = match[2]; | |
| const day = match[3]; | |
| console.log('Date -> ', year, month, day); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment