6 Functions for the journeyman: understanding function invocation
This chapter covers
- An implicit function parameter: this
- Ways of invoking functions
- Constructors
In the previous chapter, you saw that JavaScript is a programming language with significant functionally oriented characteristics. We explored the differences between function call arguments and function parameters, and how the values are transferred from call arguments to function parameters.
This chapter introduces the oft-misunderstood this keyword, which can be thought of as an implicit function parameter. Rather than having a fixed value for a given function, this varies based on how the function is called.
We’ll look at different ways of calling functions, including the call and apply methods, and how they affect this. We’ll also look at how the new keyword invokes a function as a constructor, how functions can be bound to a particular value of this, and how arrow functions can be used to sidestep this entirely.
Without further ado, let’s start exploring!