When the foo() function is called on its own without any context, the this keyword refers to the global object (Window in the browser or global in Node.js). So, the output of the first call to foo() will be Window.
In the second call, the foo() function is invoked on the obj object. The this keyword inside the foo() function now refers to the obj object, so the output will be the obj object.
Output:-
Window // output of the first call to foo()
------------------------
{ name: 'Trinits', foo: [Function: foo] } // output of obj.foo()
Download
0 formats
No download links available.
Program 14 this keyword | JavaScript this contexts | window object | current object context | NatokHD