Chapter 4. Functions for the journeyman: understanding function invocation

 

This chapter covers

  • Two implicit function parameters: arguments and this
  • Ways of invoking functions
  • Dealing with problems of function contexts

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 continues in a similar vein, by first discussing something that we kept from you in the previous chapter: the implicit function parameters this and arguments. These are silently passed to functions and can be accessed just like any other explicitly named function parameter within the function’s body.

The this parameter represents the function context, the object on which our function is invoked, whereas the arguments parameter represents all arguments that are passed in through a function call. Both parameters are vital in JavaScript code. The this parameter is one of the fundamental ingredients of object-oriented JavaScript, and the arguments parameter allows us to be creative with the arguments that are accepted by our functions. For this reason, we’ll explore some of the common pitfalls related to these implicit arguments.

Do you know?

4.1. Using implicit function parameters

4.2. Invoking functions

4.3. Fixing the problem of function contexts

4.4. Summary

4.5. Exercises