- When the function is created using the function keyword the value of
this
is the object left of the dot of the function which is called - If there is no object left of the dot then the value of
this
inside a function is often the global object (global
in node,Node.js andwindow
in a browser). I wouldn't recommend using thethis
keyword here because it is less explicit than using something likewindow
! - There exist certain constructs like arrow functions and functions created using the
Function.prototype.bind()
a function that can fix the value ofthis
. These are exceptions of the rule, but they are really helpful to fix the value ofthis
.
Example in nodeJSNode.js
Let me walk you through the outputs 1one by 1one (ignoring the first log starting from the second):