Skip to content

this Keyword

The this keyword refers to the current object — the object that is executing the current method. It’s how an object refers to itself from within its own code.

  • Allows methods to access the object’s own properties
  • Disambiguates between parameters and object properties
  • Enables method chaining (returning this)
  • Provides context for how a function is called
  • To access and modify object properties from within methods
  • To pass the current object to other methods
  • To implement method chaining (fluent interfaces)

When you say “I will do it myself,” you’re using a concept like this. In a conversation:

  • “I am speaking” → the current speaker refers to themselves
  • “My name” → the speaker’s own name
  • “I will call myself” → the speaker performs an action on themselves

Similarly, this is how an object says “me” or “my” from within its code.


this is a reference to the current object — the object on which a method is being invoked. It provides a way for methods to access the object’s own properties and methods.


Without this, you’d have to pass the object reference manually:

// Without 'this' — awkward and error-prone
function greet(user) {
return `Hello, ${user.name}!`;
}
// With 'this' — clean and natural
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}!`;
}
}

sequenceDiagram
participant Client
participant Object as Object Instance
participant This as "this" Reference
Client->>Object: object.method()
Object->>This: this = this object
This->>Object: this.property = value
This->>Object: this.otherMethod()
Object-->>Client: return value

this in JavaScript depends on how a function is called, not where it’s defined.

// 1. Method call — 'this' is the object
const obj = {
name: "Alice",
greet() {
console.log(this.name); // "Alice"
}
};
obj.greet(); // 'this' → obj
// 2. Function call — 'this' is global (or undefined in strict mode)
function showThis() {
console.log(this); // Window (non-strict) or undefined (strict)
}
showThis();
// 3. Arrow functions — 'this' is lexical (from surrounding scope)
const obj2 = {
name: "Bob",
greet: () => {
console.log(this.name); // undefined! 'this' is from outer scope
}
};
// 4. Constructor call — 'this' is the new instance
function Person(name) {
this.name = name; // 'this' → new instance
}
const p = new Person("Charlie");
// 5. Event handlers — 'this' is the element
button.addEventListener("click", function() {
console.log(this); // button element
});
function greet(greeting) {
console.log(`${greeting}, ${this.name}!`);
}
const user = { name: "David" };
// call — invoke with specific 'this' and arguments
greet.call(user, "Hello"); // "Hello, David!"
// apply — same as call, but arguments as array
greet.apply(user, ["Hi"]); // "Hi, David!"
// bind — create new function with fixed 'this'
const boundGreet = greet.bind(user);
boundGreet("Hey"); // "Hey, David!"

ContextJavaScriptTypeScriptPythonJava
MethodThe objectThe objectThe instance (self)The instance
ConstructorNew instanceNew instanceNew instance (self)New instance
Global scopewindow/globalN/AModule-levelN/A
Event handlerThe elementThe elementN/AN/A
Arrow functionLexical (outer scope)LexicalN/AN/A
Static methodThe classThe classThe class (cls)The class

class Circle {
constructor(radius) {
this.radius = radius;
}
area() {
return Math.PI * this.radius ** 2;
}
}
class Timer {
start() {
console.log("Starting...");
this.tick();
}
tick() {
console.log("Tick...");
}
}
class QueryBuilder {
constructor() { this.query = {}; }
select(fields) { this.query.fields = fields; return this; }
from(table) { this.query.table = table; return this; }
where(condition) { this.query.where = condition; return this; }
execute() {
console.log("Executing:", this.query);
}
}
new QueryBuilder()
.select(["name", "email"])
.from("users")
.where("age > 18")
.execute();
class Button {
constructor(label) {
this.label = label;
}
onClick() {
console.log(`${this.label} clicked`);
notificationCenter.notify("click", this); // Pass 'this'
}
}

  • In JavaScript, prefer arrow functions for callbacks to preserve this
  • Use bind in constructors for method callbacks
  • Be explicit — use this.property even when not required (clarity)
  • Avoid relying on this in global scope
  • Use method shorthand syntax (method() {}) in JavaScript objects
  • Use that = this pattern only in older JavaScript code (prefer arrow functions)

class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(message) {
console.log(`${this.prefix}: ${message}`);
}
startLogging() {
// ❌ Bug: 'this' is undefined in setTimeout callback
setInterval(function() {
this.log("tick"); // TypeError: this.log is not a function
}, 1000);
// ✅ Fix: Use arrow function
setInterval(() => {
this.log("tick"); // Works!
}, 1000);
}
}
// ❌ Arrow function as method — 'this' is wrong
const obj = {
name: "Test",
greet: () => {
console.log(this.name); // undefined (this is from outer scope)
}
};
// ✅ Regular function as method
const obj2 = {
name: "Test",
greet() {
console.log(this.name); // "Test"
}
};

Easy:

  1. What is the this keyword?
  2. What does this refer to in a class method?

Medium: 3. How does this behave differently in arrow functions vs regular functions? 4. What’s the difference between call, apply, and bind? 5. What does this refer to in the global scope?

Advanced: 6. Explain how this is determined in JavaScript (the 4 rules: default, implicit, explicit, new). 7. How does this work in event handlers in the browser? 8. Why does Python use self as an explicit parameter instead of a keyword like this?


AspectBehavior
thisReference to the current object
PurposeAccess object’s own properties and methods
JavaScriptDynamic — depends on how function is called
PythonExplicit self — always the instance
Java/C#Always the current instance
Arrow FunctionsLexical this (inherits from outer scope)
Method ChainingReturn this from methods
bind/call/applyExplicitly set this value

Previous Topic: Constructor → Next Topic: Four Pillars of OOP → Related Topics: Class, Object