Question about Destructuring Assignment to Pass an Object as a Function's Parameters

const profileUpdate = (profileData) => {
const { name, age, nationality, location } = profileData;
Here the destructuring points to “=profileData” which is where all the values/ info is stored.

const profileUpdate = ({ name, age, nationality, location }) => {
in the parameter/ shortened version, it does not point to “profileData” in any way. How does the program know where to pull the values/ info from w/o specifying which object it is referring to?

this will take the properties of the object passed in when the function is called

profileUpdate({name: "Olivia", age: 32, nationality: "Mexican", location: "London", job: "Programmer"})

I do understand that part, but in the example right above, it looks like variables were being created storing the object “profileData” values. In the below version, what you were referring to, you wouldn’t be able to create any variables storing a specific objects values because that object isn’t referenced. Only specific values were passed in. How do we know which object those values belong to? I feel like i am missing something. Are the two examples I gave doing the exact same thing? Why is it necessary to state the objects name in the first example but not in the second. Thanks.

Here is another example:

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 = ({ max, min }) => (max + min) / 2.0;
// Only change code above this line

When I assign a variable a value according to ({ max, min }) => (max + min) / 2.0, how does the program know where to get those values? ({ max, min }) is not referring to any specific object. In this case it is the “stats” object, but there is nothing referencing that when assigning the value to the variable. Thanks

Then a function function fn({a,b}) {//...} will basically be doing fn({a,b}=obj), and giving you a and b as variables if the obj has them (specifically called a and b), or nothing if the object hasnt those properties. But you obviously just execute it as fn(obj) mine is just an explicit example, so to say.

So now think of function ({min,max}), if I pass {a:5, b:4} those are undefined, if I pass stats or {min:5, max:45, base:55} it will extract the first two, by name/

when you have const half = stats => (stats.max +stats.min) / 2 it’s also not referring to any specific object. You can call half(stats) but also const fever = {max: 100, min: 97}; half(fever);
The program knows where to get those values based on the object passed in when the function is called

This is a simple script that you can run and use it to understand how destructuring of an object’s properties works.

Try un-commenting the last line as well.

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