freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../object-oriented-programming/understand-the-immediately-...

1.8 KiB

id title localeTitle challengeType
587d7db2367417b2b2512b8b Understand the Immediately Invoked Function Expression (IIFE) Comprender la expresión de función invocada inmediatamente (IIFE) 1

Description

Un patrón común en JavaScript es ejecutar una función tan pronto como se declara:
(function () {
  console.log("Chirp, chirp!");
})(); // this is an anonymous function expression that executes right away
// Outputs "Chirp, chirp!" immediately
Tenga en cuenta que la función no tiene nombre y no está almacenada en una variable. Los dos paréntesis () al final de la expresión de la función hacen que se ejecute o se invoque inmediatamente. Este patrón se conoce como una immediately invoked function expression o IIFE .

Instructions

Reescriba la función makeNest y elimine su llamada, por lo que es una immediately invoked function expression forma anónima ( IIFE ).

Tests

tests:
  - text: La función debe ser anónima.
    testString: 'assert(/\(\s*?function\s*?\(\s*?\)\s*?{/.test(code), "The function should be anonymous.");'
  - text: Su función debe tener paréntesis al final de la expresión para llamarla inmediatamente.
    testString: 'assert(/}\s*?\)\s*?\(\s*?\)/.test(code), "Your function should have parentheses at the end of the expression to call it immediately.");'

Challenge Seed

function makeNest() {
  console.log("A cozy nest is ready");
}

makeNest();

Solution

(function () {
  console.log("A cozy nest is ready");
})();