JavaScript what is “this”

“this” in js functions

I am getting deeper into the JS rabbit hole and as a programmer with more experience with strongly typed languages I find some things hard to grasp in languages where functions are first class elements (learn more here). One thing that is not tricky but interesting is the different values for “this” in a function. What “this” is in a function depends on how it is used. These are my brief notes about functions and “this”, feel free to comment if you see something incorrect.

As a global function: honk();

As a method: myCar.honk()

As a constructor: new Car()

Function called with honk.apply(…) or honk.call(…)

Examples with console output

Console output for above code

Share