2.1.11 Object Context Switching

A context switch is when the variable this changes.

For instance, in the code below, we have two Person objects: bigSister and hungryBrother.

On line 19, we can see hungryBrother is being passed into the call to GiveFood, which is being called on the bigSister object.

If we step into the call to to GiveFood, we can see in the locals window that this is referring to the bigSister object (Margo) because GiveFood was called on bigSister. We can also see that hungryPerson is referring to the object with the name of Billy that previously called hungryBrother.

Then, we can step into line 19, which is calling Eat on the hungryPerson object. There, we can see that this is now referring to the object with the name of Billy, which in previous contexts was hungryPerson and hungryBrother.