THIS
| javascriptSometimes I thought that was this
, but this
turned out to be something else entirely. I learned this
is not assigned a value until its containing function is invoked. Whatever object invokes the function containing this
becomes this
.
In the global scope, this
refers to the window object. However, when strict mode is enabled, this
is assigned undefined
in global functions and in anonymous functions not bound to an object.
In the example, makeJoke()
and window.makeJoke()
are equivalent. window is the invoker, so this
is assigned to window. obj.makeJoke()
on the other hand has obj as the invoker, so this
is assigned to obj. Next time, I’ll take a look at common cases where this
is misunderstood and pulls some trick-tronkory.