How do a function parameters access the object

Hello there.

Why do you have a screenshot in your code? Is that purposeful?

To answer your question:
I think you are getting confused by the naming convention(if a case of 1 can be considered a convention) used in the above.

You decide to refer to a function argument with any name you want. This reference is called a function parameter.
So, this line has determined the function parameters:

updateRecords(5439, "artist", "ABBA");

The order is important:

function updateRecords(id, prop, value) {
  // Inside the function:
  id === 5439;
  prop === 'artist';
  value === 'ABBA';
}

Now, inside the function, you are able to do this:

  1. Mutate the object by using the parameters(among other objects). This is never recommended.
  2. Have your function return a new object that is based on the original collection object, but has a few properties changed.
    Example:
//create a copy of the original object
function update(value) {
  let myObj = { ...collection };
  // Change some values
  myObj[2548].album = value;
  return myObj
}
// call the function
var updatedObj = update("New Album");

Hope this helps

1 Like