Why the need for self-invoking function in "Use Destructuring Assignment to Pass an Object as a Function's Parameters"

Tell us what’s happening:

What exactly does the following

(function() {
})();

do in the code below? And why is it needed?

Based on https://www.w3schools.com/js/js_function_definition.asp, it is a self-invoking function. But then inside the function we explicitly define the function half as well and with a parameter this time too.

We could easily just have the function declaration and not have the self-invoking function, right? Not sure what I am missing here.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

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

To make sure I’m understanding the question, you’re asking if instead of

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

})();

have something like the following?

const half = (stats.max + stats.min) / 2.0;

Not quite. I am asking why not just

function half(stats) {
    // use function argument destructuring
    return (stats.max + stats.min) / 2.0;
  };

Why is the above inside the self-invoking function -

const half = (function() {

})();

The above seems like an extra step to me, and I am not sure I am grasping the need for that. Let me know if that clears the question up? Thanks.

It’s so that the code inside (your solution) can be run with that “use strict” declaration for the benefit of the tests, it doesn’t have any purpose beyond that. Note that if you remove the whole outer function and the use strict it will generally still work fine

2 Likes

Oh ok. Thank you!

So, there’s no other case where something like this would be considered a good programming practice?

Yes, its used all over the place. It’s to do with scope (lexical scope, called a closure). It keeps the stuff inside the private, so you can do things like assign variables inside it, and they won’t be accessible in global scope. It lets you kinda lock things in (in this case, adding “use strict” to the scope in which the inner function is run in).

Example:

const counter = (function() {
  let i = 0;

  return {
    get count(){
      return i;
    },
    increment() {
      return ++i;
    }
  };
}());
> counter.count
0
> counter.increment()
1
> counter.increment()
2
> counter.increment()
3
> counter.i
undefined // i is not accessible here
> counter.count
3
1 Like