Sitemap
Cuthbert Guerrero

Cuthbert Guerrero

JavaScript: this key

2 min readNov 13, 2018

--

The this keyword is one of the confusing aspects of programming with JavaScript. Admittedly, only after reading the You Don’t Know JavaScript series of Kyle Simpson, did I fully understand the subject. This article aims to summarize what I’ve read and, hopefully, help other developers to understand JavaScript better.

Get Cuthbert Guerrero’s stories in your inbox

Join Medium for free to get updates from this writer.

The most important thing to learn when dealing with the this keyword in JavaScript is:

this binding has nothing to do with where a function is declared but has instead everything to do with the manner in which the function is called.

Knowing this we can now follow a set of rules to determine the value of this at function invocation.

“this” as the global object (Default Binding)

The default function invocation, standalone function invocation, which binds this to the global object, in Node that is the global and in the browser the window.

function foo() {  
this.a = 2;
console.log(this === global); // true, if in browser `this === window`
}
foo();
console.log(a) // 2

NOTE: When using use strict, the default binding rule will not work as this will be undefined.

'use strict';function foo() {
this.a = 2;
}
foo(); // TypeError: Cannot set property 'a' of undefined

“this” as the owning object (Implicit Binding)

When calling a function with a context object, the rule states that owning object is bind to this.

function foo() {
this.a = 3;
}
var obj1 = {
a: 2,
foo: foo
};
obj1.foo();
console.log(obj1.a); // 3

“this” as a particular object (Explicit Binding)

As you may (or may not) know, each function in JavaScript is an object and have access to the call, apply, and bind functions. Each of these methods accept as their first parameter an object where this will be bound to.

function foo() {
this.a = 2;
}

var obj1 = {}
var baz = foo.bind(obj1);

baz();
console.log(obj1.a); // 2


var obj2 = {}

foo.call(obj2);
console.log(obj2.a); // 2


var obj3 = {}
foo.apply(obj3);
console.log(obj3.a); // 2

“this” as the new object (new Binding)

When invoking a constructor function with the new operator, this will be bound to the newly created object.

function Todo(title, description) {
this.title = title;
this.description = description;
this.display = function() {
console.log(this.title + ' ' + this.description);
}
}
var todo1 = new Todo('Write blog post', 'about the `this` binding in JavaScript');
todo1.display(); // 'Write blog post about the `this` binding in JavaScript

“this” with the ES6 arrow function (Lexical Binding)

The arrow function doesn’t bind this, it uses the this with its lexical scope.

function foo() {
return () => {
this.a = 2;
}
}

var obj = {
a: 0,
foo: foo
}

obj.foo()();
console.log(obj.a);

That’s it! Everything here I learned by reading the You Don’t Know JavaScript series, specifically the this & Object Prototypes book. By following these rules, we can understand this better.

--

--