If it is a constructor than the ‘this’ keyword will refer to the newly created object. So when creating an object with the ‘new’ keyword the context of the ‘this’ keyword is the new object that was created. But if the function would have been called without the ‘new’ keyword than the context would be the global context meaning the global ‘window’ object
On an object the context is the object the function is executed on. Sounds confusing? A simple example will clear that out:
This is it! It is simple as it seems. Why there is so muxh confusion? Things can go a bit more complex when we want to use a different context than the current one. Let’s look at another example:
The context var is used here because inside the ‘dat’ function we have a different context so if we used the this keyword instead of the context variable then it would refer to ‘obj.doo’ instead of just ‘obj’
The call function first parameter is the context that will be available as the this keyword inside the called function. the rest of the parameters – will be the original function parameters:
The difference between call and apply is the way the parameters are passed:
These two calls are equivalent, the apply function parameters are just passed as an array. Why do we need that? well, sometimes we have the data as an array and this will save as the conversion, no other difference besides that.