Notice that the yeti object is represented by a hash of key-value pairs. The first key, find, has a value that is a function. That function returns the value of the second key in the yeti hash, appetite, by using the “this” keyword as shorthand for “self” or “this object”. It is strange for an object to reflect on itself like this, but as an academic exercise, it illustrates inheritance very well. Things will get stranger. For now, just know that the this keyword always refers to an object.
To prove this works, let’s call .find on the yeti.
var polar_bear = Object.create(yeti);
polar_bear.appetite = “huge”;
Not being nearly as ferocious as the yeti, we set the polar bear’s appetite to “huge”. When we call .find on polar_bear, it will go up the prototype inheritance tree searching for the .find function. When it finds it in the yeti object, it will start evaluating the function and see the keyword “this”. Because this refers to the object that called the method, i.e., the polar bear, the value returned will be the value of the polar bear’s appetite(huge) — not the yeti’s(gigantic).
We can keep going down the food chain, creating descendants of the yeti who all inherit the find function from the dreaded monster. But prototype inheritance is tricky. For each new object, if we don’t declare an appetite variable, you might think that the object would instead inherit the yeti’s appetite, too, but that is not the case.
Prototype inheritance will refer to the appetite of the last ancestor in the inheritance chain that did have an appetite.