Destructuring an Object... How does our inline function know what object?

Tell us what’s happening:
As the title goes, I am failing to understand how an inline (anonymous?) function knows which object it should destructure. If the method used the ES5 method of

const half = (stats) => {
const {min, max} = stats;
return ((min+max)/2);
}

I can see how the "half function knows that we are talking about STATS and not STATSTEST (I added that object to the code).

My question is when we destructure the object in the parameter itself, neither STATS nor STATSTEST is mentioned anywhere so how does the function know which object it should destructure in the function?

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
};

//I added to test... how does our function know which object we want to destruct?
const statsTest = {
max: 0,
min: 0
}

// Only change code below this line
const half = ({max,min}) => (max + min) / 2.0;
// Only change code above this line

Your browser information:

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

Challenge: Use Destructuring Assignment to Pass an Object as a Function’s Parameters

Link to the challenge:

The object gets passed into the function as an argument. You can see what that would look like in the second test:
half(stats)

2 Likes

Hey there @SuhailM :wave:,

This question became a really common question for newcomer and I can quite understand why… But anyway, The function DON’T actually know which object to destructure. You have to pass the object inside the function. In this case, it knows to pick stats because behind the scenes there is a function calling it which stats is being passed. It is part of the test that you can’t really see but it’s there.

image
As you can see here, the test is using the function passing in the stats object.

Hope this helps :slight_smile:

2 Likes

Oh my. That is so silly of me haha. Yes going back I see it on the left!

Thanks for the clarity. I guess I have a deeper issue with understanding different functions. The whole switch from “function functionName() {}” to making the function a VARIABLE threw me off. If you don’t mind answering, is functionName in the previous sentence essentially a variable? or is the variable a functionName? or am I just overthinking everything lol…

Aaaaand I feel like the dumbest person alive. All these different ways of making functions is getting to me. I presume if I had control of that input and changed it to half(statsTest), I will get the 0?

You will get the zero. You can test this yourself by just adding this line and looking at the console.

console.log(half(statsTest));