← Back to JavaScript Course | Chapter 6: Functions Advanced | Lesson 3 of 8

JS का this Keyword

this एक ऐसा शब्द है जिसका मतलब है 'वह चीज़ जिससे मैं अभी संबंधित हूँ', जैसे किसी वाक्य में 'मेरा' कहना। यह किसे point करता है यह इस पर निर्भर करता है कि किसी function को कैसे कॉल किया गया।
Syntax
javascript
this.property

const object = {
  method() {
    return this;  // the object
  }
};

Object में this

किसी object method के अंदर, this आमतौर पर उस object को refer करता है जिसने method को कॉल किया।

अगर वही method बाद में किसी दूसरे object को assign किया जाए या standalone कॉल किया जाए, तो this उसी हिसाब से बदल जाता है क्योंकि यह call से तय होता है, न कि इससे कि function कहाँ define किया गया था।

उदाहरण: this in an Object

javascript
const user = {
  name: "Sam",
  greet() {
    console.log(this.name);
  },
};
user.greet(); // this === user

Regular Function में this

किसी regular function में this की value इस पर निर्भर करती है कि function को कैसे कॉल किया गया। strict mode में, एक plain function call में this undefined होता है।

non-strict mode में, वही plain call इसके बजाय this को global object पर default कर देता है, जो एक कारण है कि strict mode के ज़्यादा सख्त व्यवहार को आमतौर पर prefer किया जाता है।

उदाहरण: this in a Regular Function

javascript
function show() {
  "use strict";
  console.log(this); // undefined in strict mode
}
show();

Arrow Functions में this

Arrow functions अपना खुद का this नहीं बनाते। वे surrounding scope से this इस्तेमाल करते हैं।

यही ठीक-ठीक वजह है कि arrow functions को अक्सर methods के अंदर callbacks के लिए इस्तेमाल किया जाता है — वे outer this inherit करते हैं, callback कहीं और invoke होने पर उसे खोने के बजाय।

उदाहरण: this in Arrow Functions

javascript
const user = {
  name: "Sam",
  greet() {
    setTimeout(() => console.log(this.name), 0); // arrow inherits this
  },
};
user.greet();

Classes के साथ this

किसी class method में, this आमतौर पर current object instance को refer करता है।

यही वजह है कि arrow functions खुद object methods के लिए कमज़ोर विकल्प हैं: चूंकि वे अपना खुद का this bind नहीं करते, वे instance के बजाय class body के surrounding scope से this inherit करेंगे।

उदाहरण: this With Classes

javascript
class User {
  constructor(name) { this.name = name; }
  greet() { console.log(this.name); } // this === instance
}
new User("Amit").greet();

Practice

this इस्तेमाल करते समय, पहले जांचें कि function को कैसे कॉल किया गया। यह आमतौर पर बता देता है कि this किसे refer करता है।

अगर किसी function की this value गलत लगे, तो call/apply/bind इसे explicitly override कर सकते हैं, जो अक्सर यह पुष्टि करने का सबसे तेज़ तरीका है कि असल में क्या हो रहा है।

उदाहरण: Practice

javascript
const user = { name: "Sam" };
function show() { console.log(this.name); }
show.call(user); // confirms this === user
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी object method को callback की तरह pass करना, जैसे setTimeout(obj.show, 100), इसलिए this खो जाता है और अब obj नहीं रहता।
  2. किसी object method की तरह arrow function इस्तेमाल करना, जहाँ this उस object को point नहीं करता।
  3. strict mode में किसी regular function में this इस्तेमाल करना, जहाँ यह undefined होता है, इसलिए this.name TypeError throw करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.