Curso Práctico de JavaScript

Toma las primeras clases gratis

COMPARTE ESTE ARTÍCULO Y MUESTRA LO QUE APRENDISTE

Functions

Link para conocer las 7 maneras de declarar una función:

https://dev.to/victordeandres/funciones-en-javascript-7-formas-de-declarar-una-funcion-523a

Function declaration: ejemplo

function hola(nombre){
     console.log(`Hola ${nombre}.`)
}

hola('Victor');    // => Hola Victor

Function expression o anonima: ejemplo

const ejemplo = function(...) { ... }

IIFE (Immediately Invoked Function Expression): ejemplo

( function () {
    let nombre = 'Victor'
    return `Hola ${nombre}`
})()

// Hola Victor

Shorthand method definition: ejemplo


// ------- SHORTHAND -------
const poblacion = {
  personas: [],
  add(...personas) {
    this.personas.push(...personas);
  },
  saluda(index) {
    return `Hola soy ${this.personas[index]}`;
  }
};

poblacion.add('Luis', 'Jesus', 'Victor');
poblacion.saluda(1) // => 'Hola soy Jesus'

// ------- MANERA CLÁSICA -------
const poblacion = {
  personas: [],
  add: function(...personas) {
    this.personas.push(...personas);
  },
  saluda: function(index) {
    return `Hola soy ${this.personas[index]}`;
  }
};

poblacion.add('Luis', 'Jesus', 'Victor');
poblacion.saluda(1); // => 'Hola soy Jesus'

Arrow function: ejemplo

const saluda2 = (nombre) => `Hola ${nombre}`;

console.log(saluda2('Victor'));  // => Hola Victor

// Al escribir las Arrow Functions no es necesario escribir la palabra function, la palabra return, ni las llaves.

Generator function: ejemplo

function * generatorFunction() {
  console.log('This will be executed first.');   // Line 1
  yield 'Hello, ';   // Line 2
  console.log('I will be printed after the pause');  
  yield 'World!';
}
const generatorObject = generatorFunction(); // Line 3
console.log(generatorObject.next().value); // Line 4
console.log(generatorObject.next().value); // Line 5
console.log(generatorObject.next().value); // Line 6
// This will be executed first.
// Hello, 
// I will be printed after the pause
// World!
// undefined

Function constructor: ejemplo

const sumaFunction = function(numero_1, numero_2) {
    return numero_1 + numero_2
}

// Ejecutamos nuestra función.
sumaFunction(14,123) // => 137

CREDITOS A ESTE LINK DE NOTION QUE ENCONTRE POR INTERNET: https://www.notion.so/Fundamentos-de-JavaScript-2e92f5001c5f4dd499df626dcb814f28

Curso Práctico de JavaScript

Toma las primeras clases gratis

COMPARTE ESTE ARTÍCULO Y MUESTRA LO QUE APRENDISTE

0 Comentarios

para escribir tu comentario

Artículos relacionados