O'Reilly logo
  • Ahmed Elgendy thinks this is interesting:

Shouldn’t this code just work, though? In other words, we wrote started in the fiat object; shouldn’t JavaScript be smart enough to figure out we mean the started property?

Nope. As you can see it isn’t. How can that be?

Okay, here’s the deal: what looks like a variable in the method is really a property of the object, but we aren’t telling JavaScript which object. You might say to yourself, “Well, obviously we mean THIS object, this one right here! How could there be any confusion about that?” And, yes, we want the property of this very object. In fact, there’s a keyword in JavaScript...


Cover of Head First JavaScript Programming


How to reference an object's variable while defining an object's method/function? A tip: this is very important to avoid potential errors.