ES6: Use Destructuring Assignment to Pass an Object as a Function's Parameters question

ES6: Use Destructuring Assignment to Pass an Object as a Function's Parameters question
0

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters

There is this code:

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return function half(stats) {
    // use function argument destructuring
     
    return (stats.max + stats.min) / 2.0;
  };
  // change code above this line

})();
console.log(stats); // should be object
console.log(half(stats)); // should be 28.015

Why we return a function and what can we do with it? And why we have two half functions? And why we store the function in the constant?

Can anyone please answer my questions?

@sp2012 storing a function inside a variable is one of the two ways of creating a function and it’s called a Function Expression

So in this case, the function is created and assigned to the variable explicitly

The return statement is appended to the inner function so as to make the outer function return what the inner function is returning.

And If you look very well, the outer function is a self calling function which also calls the inner function.

I hope my explanation is clear. You can let me know to clarify more if unclear to you. Thanks

1 Like

Thanks for your answer.

Why do we pass stats into the inner function? stats isn’t global?

You use function parameter instead of referencing global variables to make function reusable.

I don’t understand why the outer function returns an inner function and I don’t understand why we store the outer function in a variable. The inner function will be stored in the variable?

The why is because the tests need(ed) this kind of set-up, a local scope where to activate strict mode.

Anyway, yeah, the outer function is immediately invoked,'it returns a function which is stored in the half variable, and you can call this function as needed with half()

so the outer function is not actually being assigned to the variable half at all. Instead, that function is being run immediately. By surrounding it like this:

(function(){
  // do something...
})();

that function runs in-place. Immediately. And half is being assigned to the result of that function that has just been called, which in this case is… a function.

Why do we do that? When the outer function is run, it creates within itself a variable namespace, a context, that is private and secure from outside influence. The only thing that can affect that private namespace is… any function defined within that private namespace. In the given example, we aren’t really doing anything with that namespace (setting variables or whatnot), but that’s what it is often used for.

You can find out more by reading Addy Osmani’s great book on Design Patterns, specifically, the module pattern. Additionally, if you google IIFE (or Immedately Invoked Function Expression).

Now, our IIFE returns a function, which runs inside that private space.

However, have you deconstructed the object in the parameter yet? That was the purpose of this lesson. :wink:

You are welcome

I believe @snowmonkey helped you out. Have fun destructuring.

Thanks for the input.