This can be explained by a simple example:
In the above code sample notice the function fullName() which returns firstName and lastName and how they access the properties using this operator. Each alert defined here provides a different output and explains the function context in its true sense! Let’s explore each of the outputs –
Here fullName() is accessed as top-level function and remember the top-level functions are properties of window so the function context will be the window itself. In this execution the code will look for firstName and lastName in the window object which is actually set on top with values WindowFirstName & WindowLastName! So the output will be:
fullName is a property that is defined within persorn1 object which internally holds the reference of the function fullName. So in this case the function context will be of the calling function person2 and hence it returns values of firstName & lastName defined within the object.
In this case we are invoking the function using the call() method and specifying the first parameter as it’s function context. person2 is passed to the call() method which set’s the function context as person2. Hence the output: