To start at the beginning, we call this function
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
We are passing the function four parameters:
- The
recordCollection
, which is a reference to an object that contains all our records. Within updateRecords
, that will be assigned to the records
variable.
- the number
5439
, which is a reference to a particular record within that collection. As the collection is an object, thatâs the property name within that object. Within updateRecords
, weâll assign that to the variable id
.
- The string
'artist'
, which is a reference to a property within the record we want to add, edit or delete. Inside updateRecords
, itâll be called prop
.
- And the string
'ABBA'
, which is the value we want to use to update that particular property. Inside updateRecords
, this one will be value
.
This is the order arguments go into the function, as its been defined:
function updateRecords(records, id, prop, value){
/* function body */
}
So we know weâll have to pass in that records
object (our function doesnât know about anything outside of itself, so we have to tell it âHey, hereâs the records!â).
That records
is a reference to the recordCollection
object. If we look at it, we can see that itâs just a normal Object literal:
const recordCollection: {
2548: { /* sub-object */ },
2468: { /* sub-object */ },
1245: { /* sub-object */ },
5439: { /* sub-object */ }
}
Itâs an object with some number of things inside it. Each of those things is a record, identified by an id
. We pass in the id
of the one we want (in this case, 5439
), and we can use that particular id
to find the property we want. By using the records
variable in our function as a reference to the collection, and the id
variable as the particular one we want, records[id]
will give us records[5439]
. That looks like:
5439: {
albumTitle: 'ABBA Gold'
}
So we have the particular record we want, the id
was useful for that, but what do we want to do to it? The spec tells us we want to do something with a particular property. Which property? The one passed to our function in the prop
argument (in this case, "artist"
).
The function knows only what we tell it. Weâre provided the recordCollection
so we can see the âshapeâ of the data, by which I mean we can see what a record should contain. There are some things we can tell, from looking at both the data itself and from looking at the spec:
- Given a particular record (
records[id]
), we want to find the particular property on that object and do something with it.
- If we donât have a
value
passed in, we are removing that particular property.
- Otherwise, in all cases except for the
tracks
property, we want to simply set the property to the given value.
-
tracks
is the only odd property out. That one contains an array. If weâre adding a value to tracks
, we need to be sure thereâs an array there for us to add to before we add anything to it.
So the spec tells us what the âshapeâ of the data is, and what steps we want to take. So we start writing the updateRecords
function, knowing itâll be passed some sort of object (which weâll call records
) that follows a given structure. The structure is that we see in recordCollection
, but might or might not BE recordCollection. We arenât tied to that, as weâll be passing something in.
So we know that records
will be an object, and it will contain one or more nested objects within it. And we know that, if we want to access the tracks
property in that nested object, we have to treat that and only that as an array. Any other property will simply be deleted, created or overwritten, but tracks
will either be deleted or added to. That one is unique. Canât stress that enough.
This was a long semi-lecture, but the point is this: recordCollection
and updateRecords
are only connected because recordCollection
is being passed into updateRecords
as its first parameter.