Thursday, August 25, 2022

I find a MDN weird snippet and make two more acceptble versions

 const pet = function (name) {   // The outer function defines a variable called "name"

  const getName = function () {

    // The inner function has access to the "name" variable of the outer function

    return name;

  }

  return getName; // Return the inner function, thereby exposing it to outer scopes

}

const myPet = pet('Vivie');


myPet(); // Returns "Vivie"


----------------------------->I fix this two ways<---------------------------------------

        const pet = function (name) {
            // The outer function defines a variable called "name"
            const getName = function () {
                // The inner function has acess to the "name" variable of the outer function
                return name;
            }
            return getName(); <<<<<<---Here I put the ->() after the get name
        }

        const myPet = pet('Vivie');

        put('-->New exercise<--');
        put(myPet);

That one above was return getName and I replace for return getName()

----------------------------->Second way<---------------------------------------

        const pet = function (name) {
            // The outer function defines a variable called "name"
            const getName = function () {
                // The inner function has acess to the "name" variable of the outer function
                return name;
            }
            return getName; // Return the inner function, thereby exposing it to outer scopes
        }

        const myPet = pet('Vivie')();<<<<<----Here the change I add ()

        put('-->New exercise<--');
        put(myPet);


Was write const myPet = pet('Vivie'); and I replace for const myPet = pet('Vivie')();


myPet() is an object? So we find a rotten potato of an object that has a parenthesis like a function... do the work but the reason is still of a function so is a hybrid object that appears with a function. Scope is like 

Rute Bezerra de Menezes Gondim






No comments:

Post a Comment