Skip to content
FunctionsChapter 17 / 47Intermediate5 min read

The this Keyword

JavaScript this keyword ki value kaise tay hoti hai, method call, new, call/bind aur arrow functions ke rules, Roman Urdu mein simple examples ke saath.

On this page

1. The this Keyword kya hai?

Simple words mein, this function se poochhta hai: "mujhe kis ne bulaya?" Agar user.greet() likha to this user hai. Wahi function kisi aur tareeqe se call karo to this badal jata hai.

Sirf arrow functions is rule se bahar hain. Un ka this likhne ki jagah se tay hota hai.

2. Kyun zaroori hai?

this ki wajah se ek hi method bohot saare objects ke saath kaam kar sakta hai. Lekin rule na samjho to undefined errors aate hain.

Bina thisthis ke saath
Har object ke liye alag functionEk method, har object par chalta hai
Object ka naam hardcodethis khud current object batata hai
Class instances ka data access mushkilthis se har instance ka apna data
Copy paste wala codeReusable methods aur classes

3. Kahan use hota hai?

Areathis kya hota hai
Object methodsWoh object jis par method call hua
ClassesNaya bana hua instance
DOM event listener (normal function)Woh element jis par listener laga
Callbacks with arrowBahar wale function ka this
Library codecall, apply, bind se khud set kiya gaya this

4. Kaise kaam karta hai?

this ke rules priority ke order mein:

RuleCall ki shakalthis
new bindingnew User()Naya object
Explicitfn.call(obj), fn.bind(obj)obj
Implicitobj.fn()Dot se pehle wala obj
Defaultfn()Strict mode mein undefined
Arrow() => thisBahar wale scope ka this
this ki value kaise dhoondein
Kya yeh arrow function hai? To bahar wala this
Kya new se call hua? To naya object
Kya call, apply ya bind use hua? To diya gaya object
Kya obj.method() ki shakal hai? To obj
Warna strict mode mein undefined

Modules aur classes hamesha strict mode mein chalte hain, is liye modern code mein plain fn() call par this undefined hota hai.

5. Code example

Implicit binding aur lost this

js
const user = {
  name: "Ayesha",
  greet() {
    return `Hi, I am ${this.name}`;
  },
};

console.log(user.greet());

const greetFn = user.greet;
try {
  console.log(greetFn());
} catch (error) {
  console.log(error.message);
}

const bound = user.greet.bind(user);
console.log(bound());

Class aur arrow callback

js
class Cart {
  items = [];

  add(item) {
    this.items.push(item);
    return this;
  }

  total() {
    return this.items.reduce((sum, item) => sum + item.price, 0);
  }
}

const cart = new Cart();
cart.add({ price: 300 }).add({ price: 200 });
console.log(cart.total());

Code kaise kaam kar raha hai?

CheezMatlab
user.greet()Implicit binding, this = user
greetFn()Method alag ho gaya, this undefined, error aata hai (module mein)
bind(user)Naya function jis ka this hamesha user
new Cart()this naya cart instance
return thisMethod chaining mumkin hoti hai
Arrow in reduceApna this nahi, lekin yahan zaroorat bhi nahi

Interview tip

this define karte waqt nahi, call karte waqt tay hota hai (arrow ke ilawa). Interview mein code dekh kar pehle call site dhoondein, phir upar wali priority list lagayein.

6. Common mistakes

Galati: Method ko callback mein seedha pass karna

Why? setTimeout(user.greet, 100) mein method object se alag ho jata hai aur this kho jata hai.

Behtar: Arrow wrapper do, jaise setTimeout(() => user.greet(), 100), ya user.greet.bind(user) use karo.

Galati: Object method ko arrow function banana

Why? Arrow ka this object nahi balkay bahar wala scope hota hai, this.name undefined aata hai.

Behtar: Methods ke liye method shorthand likho: greet() aur andar this.

Galati: Event listener mein arrow aur this se element expect karna

Why? Arrow function ka this element nahi hota, is liye this.classList kaam nahi karta.

Behtar: Element ke liye event.currentTarget use karo, ye arrow aur normal dono mein sahi hai.

7. Pehle aur baad mein kya seekhein?

Suggested order
Objects
The this Keyword
call, apply & bind
Classes

Final quick revision

PointDetail
this kab tay hota haiCall time par
obj.fn()this = obj
new Fn()this = naya object
call / apply / bindthis khud set karo
Plain fn() strict modeundefined
Arrow functionBahar wala this