Facebook Pixel

Default and Implicit this Binding in JavaScript

Plain calls and method calls. Here is how default and implicit binding work.

Default and Implicit this Binding in JavaScript

this has two common binding rules: default (plain call) and implicit (method call). Here is how each works.

Default Binding (Plain Call)

function foo() { console.log(this); } foo(); // window (non-strict) or undefined (strict) `` In non-strict mode, `this` is the global object (`window` in browsers). In strict mode, `this` is `undefined`. ### Implicit Binding (Method Call) ```js const obj = { name: "Kunal", greet() { console.log(this.name); }, }; obj.greet(); // "Kunal" `` The object to the left of the dot at **call time** determines `this`. `obj.greet()` means `this` is `obj`. ### Losing `this` (Detaching a Method) ```js const obj = { name: "Kunal", greet() { console.log(this.name); } }; const fn = obj.greet; fn(); // undefined (this is lost plain call) `` When you assign a method to a variable and call it, it is a plain call. `this` is not `obj` anymore. ### Callbacks Lose `this` ```js const obj = { name: "Kunal", greet() { console.log(this.name); } }; setTimeout(obj.greet, 1000); // undefined (detached, plain call) `` `setTimeout` calls `greet` as a plain function, not as a method of `obj`. `this` is lost. ### Fix: bind, Arrow, or Wrapper ```js // bind setTimeout(obj.greet.bind(obj), 1000); // "Kunal" // arrow wrapper setTimeout(() => obj.greet(), 1000); // "Kunal" // arrow method const obj2 = { name: "Kunal", greet: () => console.log(this.name), // lexical this (be careful) }; `` ### Nested Objects ```js const outer = { inner: { name: "Kunal", greet() { console.log(this.name); }, }, }; outer.inner.greet(); // "Kunal" (this is inner, the immediate object) `` Only the last object before the method matters. ### The Takeaway Default binding: plain call, `this` is global/undefined. Implicit binding: method call, `this` is the object to the left of the dot. Detaching a method (assigning to a variable or passing as a callback) loses `this`. Fix with `bind`, arrow wrapper, or arrow method.

When a function is called as a plain function (not as a method), this is the global object in non-strict mode, or undefined in strict mode. This is the lowest-priority binding.

When a function is called as a method (obj.method()), this is the object to the left of the dot at call time. The object that owns the method call determines this.

Because the method is detached from obj when passed as a reference. setTimeout calls it as a plain function, not as a method of obj. this becomes global or undefined. Fix with .bind(obj) or an arrow wrapper.

Use .bind(obj) to permanently bind this, or wrap the method call in an arrow function (() => obj.method()), or define the method as an arrow in the object. Arrows inherit this lexically.

Only the last object before the method call. For outer.inner.greet(), this is inner (the immediate object), not outer. The object to the left of the dot at call time is this.

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.