Understanding the “this” keyword in Javascript

The this keyword always refers to an object.

We can use it outside of any function in the global scope but most often it is used inside a function (otherwise known as a method, when the function is defined inside an object).

When it is used within a method, it refers to the object on which the method is being invoked.

This is also true of a global function because a global function is actually just a method of the window object, which is the default global object in a web browser.

All functions are also objects, and just like any other objects they can have properties.

When a function is called (or it executes), it receives a special property called this, which is a variable with the value of the object that invokes the function.

Until the introduction of Javascript’s newer Arrow functions (which we will cover later), it was common to think that the value of this depends on the object where the function is defined. But this is not the case. The value of this, when inside a function, is not determined until the function actually executes. The value assigned to this, in most cases, depends on the object which invokes that function.

The value of this is determined using a simple series of steps:

If the function is invoked using Function.call or Function.apply, this will be set to the first argument passed to call/apply. If the first argument passed to call/apply is null or undefined, this will refer to the global object (which is the window object in Web browsers).

If the function being invoked was created using Function.bind, this will be the first argument that was passed to bind at the time the function was created.

If the function is being invoked as a method of an object, this will refer to that object.

Otherwise, the function is being invoked as a standalone function not attached to any object, and this will refer to the global object.

The object that the function is bound to, attached to, or is invoked by, is what is regarded as the execution context.????

Lets start with the ECMAScript definition of this:
The this keyword evaluates to the value of the ThisBinding of the current execution context.

this refers to execution context and the execution context refers to the object which is bound to the current scope????