- Published on
Mastering the 'this' Keyword in JavaScript
- Authors

- Name
- Yinhuan Yuan
Introduction
The this keyword in JavaScript is a powerful feature that allows for flexible and reusable code. However, it's also one of the most misunderstood concepts in the language. In this blog post, we'll demystify this, explore its behavior in different contexts, and learn how to use it effectively.
- What is 'this'?
- The Rules of 'this'
- 1. 'new' Keyword
- 2. call(), apply(), and bind()
- 3. Method Invocation
- 4. Global Context
- Arrow Functions and 'this'
- Common Pitfalls
- Best Practices
- ES6 Class Syntax and 'this'
- Conclusion
What is 'this'?
In JavaScript, this is a special keyword that refers to the context in which a function is executed. Unlike many other programming languages, the value of this is not determined by how a function is defined, but by how it's called.
The Rules of 'this'
The value of this is determined by the following rules, in order of precedence:
newkeywordcall(),apply(), orbind()methods- Method invocation
- Global context
Let's explore each of these in detail.
1. 'new' Keyword
When a function is invoked with the new keyword, this inside the function refers to the newly created object:
function Person(name) {
this.name = name
}
const john = new Person('John')
console.log(john.name) // "John"
2. call(), apply(), and bind()
These methods allow you to explicitly set the value of this:
function greet(name) {
console.log(`Hello, ${name}, ${this.name}`)
}
const person = { name: 'Alice' }
greet.call(person, 'Bob') // "Hello, Bob, Alice"
greet.apply(person, ['Bob']) // "Hello, Bob, Alice"
const boundGreet = greet.bind(person)
boundGreet('Bob') // "Hello, Bob, Alice"
3. Method Invocation
When a function is called as a method of an object, this refers to the object the method is called on:
const obj = {
name: 'Bob',
greet() {
console.log(`Hello, ${this.name}!`)
},
}
obj.greet() // "Hello, Bob!"
4. Global Context
In the global context (or inside a function in non-strict mode), this refers to the global object (window in browsers, global in Node.js):
console.log(this === window) // true (in a browser)
function globalThis() {
return this
}
console.log(globalThis() === window) // true (in non-strict mode)
In strict mode, this is undefined in functions that are not methods or constructors:
'use strict'
function strictThis() {
return this
}
console.log(strictThis() === undefined) // true
Arrow Functions and 'this'
Arrow functions don't have their own this binding. Instead, they inherit this from the enclosing scope:
const obj = {
name: 'Charlie',
greet: function () {
setTimeout(() => {
console.log(`Hello, ${this.name}!`)
}, 100)
},
}
obj.greet() // "Hello, Charlie!" (after 100ms)
This behavior makes arrow functions particularly useful for callbacks and methods that need to access this from their containing scope.
Common Pitfalls
1. Losing 'this' in Callbacks
const obj = {
name: 'David',
greet() {
setTimeout(function () {
console.log(`Hello, ${this.name}!`)
}, 100)
},
}
obj.greet() // "Hello, undefined!" (after 100ms)
To fix this, you can use an arrow function, bind this, or pass this as an additional argument.
2. Method Assignment
const obj = {
name: 'Eve',
greet() {
console.log(`Hello, ${this.name}!`)
},
}
const greet = obj.greet
greet() // "Hello, undefined!"
The context is lost when the method is assigned to a variable. To preserve it, you can use bind() or call the method directly on the object.
Best Practices
- Use arrow functions for callbacks and methods that don't need their own
thiscontext. - Be explicit about
thisbinding usingcall(),apply(), orbind()when necessary. - Avoid using
thisin the global scope. - Use strict mode to catch unintended global object references.
ES6 Class Syntax and 'this'
ES6 introduced the class syntax, which provides a more intuitive way to work with this in object-oriented programming:
class Person {
constructor(name) {
this.name = name
}
greet() {
console.log(`Hello, ${this.name}!`)
}
}
const frank = new Person('Frank')
frank.greet() // "Hello, Frank!"
In class methods, this refers to the instance of the class, similar to method invocation on objects.
Conclusion
Understanding this in JavaScript is crucial for writing effective and reusable code. Remember that the value of this is determined by how a function is called, not how it's defined. By mastering the rules of this and being aware of common pitfalls, you can leverage this powerful feature to write more flexible and maintainable JavaScript code.
As you continue to work with JavaScript, pay attention to the context in which functions are executed, and you'll find that this becomes a powerful tool in your programming toolkit rather than a source of confusion.
Happy coding, and may your this always point where you expect it to!