Passing destructured object to function


const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};

// Only change code below this line
const half = (stats) => (stats.max + stats.min) / 2.0; 
// Only change code above this line

I’m having trouble understanding how passing a destructured object as arguments to a function works. In this particular challenge, you have to destructure the stats object and pass max and min as functions to the function below. So the code would look like this:


const stats = {

  max: 56.78,

  standard_deviation: 4.34,

  median: 34.54,

  mode: 23.87,

  min: -0.75,

  average: 35.85

};

const half = ({max,min}) => (max + min) / 2.0;





My question is what if we had another object with the same properties but different property values. How would the function determine which object it should take its’ arguments from?

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

Link to the challenge:

It doesn’t care. We are just passing an object. In the original code we are calling it “stats”. It is just a coincidence that it has the same name in the global scope. But the function doesn’t know or doesn’t care what it was called (if it even had a name) when it was passed - it just accepts what was sent and stores it in that variable.

The same thing happens with the destructuring - except that the object isn’t stored. The function just looks for those properties on the anonymous object that is passed. Which object is used? Whichever is passed. You can pass it one and it will use that. Then you can call it with a different object and it will use that. It doesn’t care.

Does that help?

Thank you for your effort but I just realized that I made a typo. Instead of “what if we had another function” I meant to say what if we had another object. I have now edited the original post. Anyhow I think my question still remains. How would our function know which object to take it’s arguments from, if there are two objects with the same properties but different values?

Please don’t do that because it messes with the flow of the thread. But I don’t think it matters in this case because I think that is how I interpreted what you wrote. The answer is the same.

const showProp1 = obj => console.log(obj.prop1)
// const showProp1 = ({ prop1 }) => console.log(prop1)

showProp1({ prop1: 111 })
// 111

showProp1({ prop1: 222 })
// 222

const obj1 = { prop1: 333 }
const obj2 = { prop1: 444 }

showProp1(obj1)
// 333

showProp1(obj2)
// 444

const funcThatReturnsObject = () => ({ prop1: 555 })

showProp1(funcThatReturnsObject())
// 555

showProp1({ somethingElse: 666 })
// undefined

It doesn’t know or care where the object comes from. It doesn’t know or care if it was names or what the name was. All it knows is that it has a reference (memory address) that points to an object. If it is the first version of showProp1 it will store that reference in a local variable called “obj” because we told it to call it that. If it is the second version of showProp1 then it will look in the object to which that reference points and look for a prop called “prop1” and store that value in a local variable with the same name.

Every function call is different. In the above examples it is never called with the same reference twice. It doesn’t “remember” or care.

Does that help?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.