Quests for Knowledge

Exploring JavaScript's Function Invocation Patterns

No comments
In chapter 4 of JavaScript: The Good Parts, the author writes about JavaScript's take on functions. I think that I've read the first few parts of the chapter a couple of times now and I plan on spending more time on this chapter before I move on. There are some very interesting parts discussed in this chapter and I want to make sure that I get the chance to actively learn the concepts through testing as opposed to just reading about them.

Here's my attempt at describing JavaScript's Function Invocation Patterns. First, a code sample. 


As shown in the sample, there are 4 invocation patterns. Each invocation pattern has its own way of defining what the this keyword represents in the body of the function. 

Method Invocation: 
How: An object has a method and it's called with dot syntax. someObject.do_something()
this = the object itself

Function Invocation:
How: A function is defined in the global space or within another function. 
this = global object

Constructor Invocation:
How: A function is created to mock a constructor and the new keyword is used to create an instance.
this = the instantiated object

Apply Invocation:
How: .apply() is added to the end of a function allowing to pass in an object and parameters.
this = the object passed in

No comments :

Post a Comment