In your examples folder, open up the context sub folder. And let's look at the context basic example. Here I have two objects, foo and bar. Each one of these objects has two properties, a greeting property, and a greet property. Now with objects when a property happens to be a function, it's called a method. So each one of these objects has a greet method.
So the Foo dot greet or the greet method of foo, returns this greeting. Now, in a method, this keyword is very important. It refers to the function the object to which a method belongs. So this keyword on line number five refers to foo, because this method greet belongs to this object foo. So once again, the JavaScript this key Word inside of a method refers to the object to which that method belongs. On line number five, describe greeting, resolves to food greeting, on line number 13.
This talk greeting resolves to bardock greeting. So our console log outputs should equal I am foo, and I am bar because we're calling foo dot greet, and bar dot greet. So let's see this in action. If I open up, block scope. Context place for example, I see I am foo and I am bar. And that's exactly what we expect.
Now, there's something we can do that might make this a little bit clearer. Inside of the bar dot greet method. I'm going to put a console during statements I'm going to say console during this. I'm going to just inspect this so we can just get a little bit deeper understanding of what this means online and 13 so now when I rerun the code, I see this object in my console. And I can see it's got two properties greet, which is a function and greeting I am bar. And if I do if I take this and move it inside of foo, foo dot greedy greet.
When I rerun this code, now it's object you can see it's kind of greet method and a greeting. I am foo. So the thing to remember about context is that it is the object to which a method belongs. So inside of a method, the JavaScript this keyword can be used to reference the object to which that method belongs here inside of foo, that this keyword references foo, here inside of bar that this keyword references bar. So this greeting, resolves to bar greeting and this time Greeting on line five resolves to food greeting