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 this | this ke saath |
|---|---|
| Har object ke liye alag function | Ek method, har object par chalta hai |
| Object ka naam hardcode | this khud current object batata hai |
| Class instances ka data access mushkil | this se har instance ka apna data |
| Copy paste wala code | Reusable methods aur classes |
3. Kahan use hota hai?
| Area | this kya hota hai |
|---|---|
| Object methods | Woh object jis par method call hua |
| Classes | Naya bana hua instance |
| DOM event listener (normal function) | Woh element jis par listener laga |
| Callbacks with arrow | Bahar wale function ka this |
| Library code | call, apply, bind se khud set kiya gaya this |
4. Kaise kaam karta hai?
this ke rules priority ke order mein:
| Rule | Call ki shakal | this |
|---|---|---|
| new binding | new User() | Naya object |
| Explicit | fn.call(obj), fn.bind(obj) | obj |
| Implicit | obj.fn() | Dot se pehle wala obj |
| Default | fn() | Strict mode mein undefined |
| Arrow | () => this | Bahar wale scope ka this |
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
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
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?
| Cheez | Matlab |
|---|---|
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 this | Method chaining mumkin hoti hai |
Arrow in reduce | Apna 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?
| Stage | Topics |
|---|---|
| Pehle | Functions, Arrow Functions, Objects |
| Saath mein | call, apply & bind |
| Baad mein | Classes, Prototypes & Inheritance |
Final quick revision
| Point | Detail |
|---|---|
this kab tay hota hai | Call time par |
obj.fn() | this = obj |
new Fn() | this = naya object |
call / apply / bind | this khud set karo |
Plain fn() strict mode | undefined |
| Arrow function | Bahar wala this |