Function invocation was last on my JS plate and it isn’t fully swallowed yet. The implicit parameters passed along silently whenever a function is invoked, needed a bit more reading. It’s the arguments parameter and the magic this word. I think I got the arguments part. However, what this is or does, is much more complex.

this, at first I thought, is also selfexplaining, it is what it says. And that’s not wrong. It is a reference, something like the stage on which what the function does, is happening, and it indeed is also called a function’s context. But once more, the important parts are in the details.

In JS what this refers to or implies, is defined by the way a function is invoked (which was - as I have to repeat to myself - as either function, method, constructor or via call/apply), not by the way a function is declared (which was - as I have to repeat to myself - anonymously, named or defined as a variable). “Invocation context” is a word mentioned in the JS ninja book to describe what this refers to and I will try to remember it, when it comes to this.

###The context of this depending on how a function is invoked in JS:

####This in case of invocation as a function: The context is the global context, the window. It doesn’t need to be specified further. Point out.

####This in case of invocation as a method: Then the invoked function is the method of an object, and the context of this is the context of that object, not the window.
Well, to be really specific, a function invoked just plain and simple, without further specification, is invoked as a method as well, only that the object it is a method of is the global window, and that doesn’t need to be declared explicitly. It just is. Which is why these first tow ways of invocation are usually differentiated.

####This in case of invocation as a constructor: Well this is delicate. Because the concept of a function being invoked as a constructor is sort of new to me. For a function declared in the simple way like

  function workingGreat ( ) { return true; };

invoking it as a constructor would look like this:

  new workingGreat ( );

which actually creates a new object out of whatever the function “workingGreat” delivers.

What this in this particular way refers to then, is this newly created object. Hm. I think that will need a blog post on it’s own some time soon. Not sure I got the full grip on what that actually means.

####This in case of invocation via apply or call: Now this gets even trickier. By using apply or call to invoke a function, an object can be passed over to it. In a totally meaningless manner, such code could look like:

  workingGreat.apply( something, [0,1,2,3] );

or

  workingGreat.call( something, 0,1,2,3 );

with something being the object that the context of the function is referring to then when using this, and the numbers the invocation arguments, for apply as an array, for call as a simple list.

Yeah, well, many more blog post need to come … And they will. This this stuff seems like not much, but it is actually a lot for me to understand. So I leave it with this half of a donut for today.