Facebook Pixel

this in ES6 Classes in JavaScript

Class methods follow regular this rules. Here is how this works in classes and the arrow fix.

this in ES6 Classes in JavaScript

ES6 classes follow the same this rules as regular functions. Here is how this works in classes and the common pitfalls.

Basic Class

class Person { constructor(name) { this.name = name; } greet() { console.log(this.name); } } const p = new Person("Kunal"); p.greet(); // "Kunal" (this is p) `` `this` in the constructor is the new instance. `this` in methods is the instance when called as `p.greet()`. ### Losing this in Callbacks ```js class Person { constructor(name) { this.name = name; } greet() { console.log(this.name); } delayedGreet() { setTimeout(this.greet, 1000); // undefined (this is lost) } } const p = new Person("Kunal"); p.delayedGreet(); // undefined (or error) `` `this.greet` is detached when passed to `setTimeout`. `this` inside `greet` is not `p`. ### Fix 1: bind in the Constructor ```js class Person { constructor(name) { this.name = name; this.greet = this.greet.bind(this); // bind in constructor } greet() { console.log(this.name); } delayedGreet() { setTimeout(this.greet, 1000); // "Kunal" (now bound) } } `` ### Fix 2: Arrow Method ```js class Person { constructor(name) { this.name = name; } greet = () => { console.log(this.name); // lexical this (the instance) }; delayedGreet() { setTimeout(this.greet, 1000); // "Kunal" } } `` Arrow methods (class fields) capture `this` from the constructor's scope. They are per-instance (not on the prototype), so they cost a bit more memory but are always bound. ### Fix 3: Arrow Wrapper in the Callback ```js delayedGreet() { setTimeout(() => this.greet(), 1000); // "Kunal" } `` ### React Class Components (Legacy) Before hooks, React class components needed `bind` or arrow methods for event handlers: ```js class MyComponent extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); } handleClick() { ... } } `` Or use arrow class fields: ```js class MyComponent extends React.Component { handleClick = () => { ... }; } `` ### The Takeaway Class methods follow regular `this` rules (implicit binding). Detaching a method (passing to `setTimeout`, event listener) loses `this`. Fix with `bind` in the constructor, arrow class fields (lexical `this`), or arrow wrappers in callbacks. Arrow fields are per-instance but always bound.

this in the constructor is the new instance. this in methods is the instance when called as instance.method(). But detaching a method (passing to setTimeout or addEventListener) loses this. Fix with bind, arrow class fields, or arrow wrappers.

Because the method is detached from the instance when passed as a reference. The callback is called as a plain function, so this is not the instance. Fix by binding in the constructor or using arrow class fields.

Three ways: bind in the constructor (this.handleClick = this.handleClick.bind(this)), use arrow class fields (handleClick = () => { ... }), or use arrow wrappers in callbacks (setTimeout(() => this.method(), 1000)).

Class fields defined as arrows (handleClick = () => { ... }). They capture this from the constructor's scope (the instance), so they are always bound. They are per-instance (not on the prototype), so they cost a bit more memory.

Because React passes event handlers as regular functions (detached from the instance). Without bind, this is not the component. Fix with bind in the constructor or arrow class fields. Modern React uses hooks (function components), which avoid this issue.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.