this:
Objects store data, but to reference and access this data we use the this
keyword, as we saw on the previous pages.
We can also call these details using the name of the function and the keys (hitchhikers.key()
):
So why it this
so useful?
Maybe we are working on the code and we update the name of the Object
but forget to change the code to be executed:
But, if we use the this
keyword, the not-updated code to be executed still runs:
However, make sure to us this
within its object. Otherwise if we try not to, as in the code below, we call the [Object Window]
.
(Open console
and then click the arrow to see the results)
this
is set when we call (execute) the function, not when we define it.
What does this mean?
That it is not hoisted.
Let's create a variable, set that vairable as a function expression of the original Object
and then call the function: