# The this keyword in JavaScript

Many OOP language provide this to reference instance at runtime, as well as JavaScript. Because of the dynamic feature of JS, figuring out what this points to can be hard sometimes. This passage is aiming at helping to assert this in different contexts.

### The Fundamental

The this keyword points to the current execution context at most time. That sounds obscure, but don’t worry. Let’s see some examples to understand it.

As we can see, this points to the obj. The function func is invoked by obj.func(), so the execution context is obj.

Then, what will this evaluates without any function wrapped?
Actually, the global object(global in NodeJS and window in browsers) is the execution context in this situation. Following snippet confirms that.

### In Event Handlers

In DOM event handlers, this points to the triggered event.

While in inline event handlers, this points to correspond element.

### In Constructors

When a function is used as a constructor, this will point to the new object being constructed.

### Do Something Magic

In the first section of this passage, I say ... at most time. In this section, I’d like to show you how to change the usual behavior of this.

##### apply & call

We can bind this to a particular object by apply or call function.

We can figure out that concat and obj have no relationship when they are defined, but by using apply we can set this in concat points to obj!

When there is no parameter in function, call behaves as well as apply; the only difference between them is that apply accept an array as parameters while call accept a parameters sequence.

##### bind

There is also a method to assign a function to an object permanently.