Yes, I know, this is almost the atomic layer, but I need to repeat these things over and over again:

Declaring a function:
Announcing it, naming it, if wanted, assign it to a variable and write down within the curly {} brackets what it serves for.

Invoking a function:
Making the function do it’s job.

Just as there are several ways of declaring a function that come with tiny but important differences, there are several different ways to invoke a function and again, each with different impact on how the function’s code is treated. Maybe treating is not the proper word here. What is different depending on the way of invoking seems to mostly affect what this within a function refers to.

The word this is used in JS functions a lot and I knew, that it is sort of a reference of location or maybe rather the context the code of that particular function is happening in.

These are the few ways a function can be invoked in JS:

  1. As a function.
  2. As a method.
  3. As a constructor.
  4. And, by using apply() or call() method, which are special ways.

When a function is declared a set of arguments can be passed on within the parenthesis following the declaration. When invoking that function (at least when using ways 1.-3.), parameters for these arguments are handed in within the parenthesis following the invokation keyword.

function example (argument1, argument2, argument3) {
  var string = 'something cool would happen here with '
              + argument1 + ' and '
              + argument2 + '.\n'
              + 'but what if argument3 doesnt exist?\nWell JS simply knows its: ' + argument3;
  return string;

console.log(example('AAA', 'BBB'));

Which is showing in the browser console:

arguments of a function

In this super simple example, the declaration sets up two arguments, and when invoking the function as a function two dummy strings are handed in as parameters, AAA and BBB and the function uses these as the arguments it had asked for. It expects a third argument, but doesn’t get one. Instead of throwing an error, JS simply labels it with: undefinded. Easy peasy and logic to some extent.

But then comes the crucial thing. Apart from what happens in case of more arguments than parameters, or vice versa, the ninja describes this funny thing: Blindly, unannounced and just as built in thing, any function invocation passes two parameters: arguments and this.

So here it is, the magic this word that fell in the previous paragraph already. I can sense, that now things are becoming more juicy, more programmer, more the real thing …

But while reading and trying out I got lost in trying to log out the arguments of a function by accessing it like an array. If only I had read further in the book first. Because, arguments passed on to the funtion are array like, but not really. And now my holy ninja-ing time is already over for today. Damn. Will get back to it soon.