this keyword in objects In Hindi

this keyword in objects In Hindi

जावास्क्रिप्ट प्रोग्रामिंग में this कीवर्ड एक रिजर्व्ड कीवर्ड है, जहा this कीवर्ड मौजूदा जावास्क्रिप्ट प्रोग्राम में एक्टिव वैल्यू रेफ़्रेन्स को इंडीकेट करता है. जैसे जावास्क्रिप्ट फंक्शन प्रोग्राम में किसी फ़ंक्शन को कॉल किया जाता है। स्पेशली, किसी फंक्शन डिक्लेअर ऑब्जेक्ट मेथड में, this कीवर्ड ऑटोमेटिकली फंक्शन वैल्यू वेरिएबल ऑब्जेक्ट के रेफ़्रेन्स को इंडीकेट करता है। फंक्शन प्रोग्राम में this कीवर्ड कैसे काम करता है, यहाँ this कीवर्ड का उपयोग फंक्शन ऑब्जेक्ट के प्रॉपर्टीज और मेथड्स को एक्सेस करने में किया जाता है।

this keyword in objects In Hindi

this inside object methods in JavaScript.

जब जावास्क्रिप्ट प्रोग्रामर किसी फंक्शन ऑब्जेक्ट के अंदर की मेथड को डिफाइन करता हैं, तो उस फंक्शन मेथड के अंदर this कीवर्ड ऑटोमेटिकली उस फंक्शन ऑब्जेक्ट को रेफ़्रेन्स करता है, जहा this मेथड का उपयोग कर ऑब्जेक्ट पॉपर्टीज़ और मेथड्स को प्रोसेस और एक्सेस कर सकते हैं।

Example of this inside object methods.

let employee = {

  empname: “Siddhi”,

  empid: 01,

  empinfo() {

    console.log(“Hi, employee name is – ” + this.empname + ” and employee id is – ” + this.empid + “”);

  }

};

employee.empinfo();  // Result – Hi, employee name is – Siddhi and employee id is – 1

Explanation of this inside object methods.

इस प्रोग्राम में empinfo() मेथड में, this employee ऑब्जेक्ट को रेफ़्रेन्स करता है। यह मेथड को employee ऑब्जेक्ट के empname और empid प्रॉपर्टीज को एक्सेस करने की परमिशन प्रोवाइड करते है।

How does the JavaScript this keyword work in object methods.

this कीवर्ड जावास्क्रिप्ट फंक्शन प्रोग्राम में किसी ऑब्जेक्ट मेथड के अंदर, यह उस ऑब्जेक्ट और उसके पॉपर्टीज़ को रेफ़्रेन्स करता है, जिससे वह जुड़ा हुआ है।

this कीवर्ड का उपयोग किसी ऑब्जेक्ट या फ़ंक्शन के बाहर, इसका वैल्यू उस फ़ंक्शन के कॉल करने के आर्डर रेफ़्रेन्स पर डिपेंड करता है।

Role of this keyword in different contexts in JavaScript.

जावास्क्रिप्ट फंक्शन प्रोग्राम में किसी फ़ंक्शन के कॉल करने के आर्डर या पैटर्न के आधार पर इसकी वैल्यू मॉडिफाई या रिप्लेस हो सकती है। this कीवर्ड मल्टीप्ल कंडीशन में अलग अलग बिहेव करता है।

This keyword inside object methods.

जावास्क्रिप्ट फंक्शन प्रोग्राम में जब किसी ऑब्जेक्ट से रिलेटेड मेथड के अंदर this कीवर्ड का उपयोग किया जाता है, तो यह उस फंक्शन ऑब्जेक्ट के प्रॉपर्टीज को रेफ़्रेन्स इंडीकेट करता है।

Example of this keyword inside object methods.

let employee = {

  empname: “David”,

  employeeinfo() {

    console.log(“Hi, employee name is – ” + this.empname);

  }

};

employee.employeeinfo();  // Result – Hi, employee name is – David

Explanation of this keyword inside object methods.

इस प्रोग्राम में this.empname, employee फंक्शन ऑब्जेक्ट के empname पॉपर्टीज़ वैल्यू के रेफ़्रेन्स को इंडीकेट करता है।

This keyword in JavaScript is global context.

जावास्क्रिप्ट प्रोग्राम में किसी भी फ़ंक्शन या ऑब्जेक्ट के बाहर, this कीवर्ड फंक्शन में ग्लोबल ऑब्जेक्ट प्रॉपर्टीज रेफ़्रेन्स को इंडीकेट करता है। यह वेब ब्राउज़र में यह विंडो ऑब्जेक्ट में प्रीव्यू होगा।

console.log(this); // This in a web browser, this will print the window object.

Explanation of This keyword in JavaScript is global context.

इस कंसोल स्क्रीन में ग्लोबल एक्सेक्युशन रेफ़्रेन्स किसी भी फ़ंक्शन या ऑब्जेक्ट के बाहर डिक्लेअर आर्डर में, यह ग्लोबल फंक्शन ऑब्जेक्ट वेब ब्राउज़र विंडो के रेफ़्रेन्स को इंडीकेट करता है।

JavaScript this keyword inside a regular function.

जावास्क्रिप्ट फंक्शन में एक रेगुलर फ़ंक्शन एरो फ़ंक्शन नही में है, पर इसका वैल्यू इस बात पर डिपेंड करता है कि, फ़ंक्शन को कैसे प्रोग्राम में कॉल किया जाता है। यदि मौजूदा प्रोग्राम में फ़ंक्शन को किसी ऑब्जेक्ट से जुड़े बिना कॉल करते है, तो यह ग्लोबल ऑब्जेक्ट (गैर-सख्त मोड में) के रेफ़्रेन्स को इंडीकेट करेगा या सख्त मोड में अपरिभाषित मोड में होगा।

Example of this keyword inside a regular function.

function display() {

  console.log(this);  // here this In a non-strict mode, it will be the global object

}

display();

Explanation of this keyword inside a regular function.

यहाँ यह फ़ंक्शन प्रोग्राम में किसी ऑब्जेक्ट का पार्ट नहीं है, इस वजह से यह ग्लोबल ऑब्जेक्ट ब्राउज़र में विंडो को रेफ़्रेन्स में इंडीकेट करता है।

JavaScript this keyword inside arrow function.

जावास्क्रिप्ट फंक्शन में एरो फ़ंक्शन में एक लिट्रल this कीवर्ड है, जिसका अर्थ है कि this कीवर्ड के आसपास के रेफ़्रेन्स से इंडीकेट करता है, न कि एरो में this से फ़ंक्शन को कैसे कॉल किया जाता है।

Example of this keyword inside arrow function.

let employee = {

  empname: “Harry”,

  empinfo: () => {

    console.log(this.empname);  // here ‘this’ keyword refers to the surrounding context, not the ’employee’ object.

  }

};

employee.empinfo();  // Result – undefined

Explanation of JavaScript this keyword inside arrow function.

इस उदाहरण में, एरो फ़ंक्शन अपने this रेफ़्रेन्स को यूज़ नहीं करता है। बल्कि, यह आसपास के रेफ़्रेन्स से this कीवर्ड से इंडीकेट करता है। जबकि empinfo मेथड एक एरो फ़ंक्शन है, यहाँ this कीवर्ड employee ऑब्जेक्ट से बंधा नहीं है, और यह अंत में एक ग्लोबल ऑब्जेक्ट या सख्त मोड में अपरिभाषित हो जाता है, जिसमें empname पॉपर्टीज़ नहीं है।

this keyword in constructor function (class) in JavaScript.

कन्स्ट्रक्टर फ़ंक्शन या ES6 में क्लास में, this कीवर्ड उस ऑब्जेक्ट के इंस्टेंस को रेफ़्रेन्स करता है, जिसे बनाया जा रहा है।

Example of this keyword in constructor function (class).

function employee(name, id) {

  this.empname = name;

  this.empid = id;

}

let employee1 = new employee(“Siddhi”, 4);

console.log(employee1.empname);  // Result – Siddhi

console.log(employee1.empid);  // Result – 4

Explanation of this keyword in constructor function (class).

यहाँ कंस्ट्रक्टर फ़ंक्शन के अंदर, this कीवर्ड से नए बनाए गए फंक्शन ऑब्जेक्ट के रेफ़्रेन्स को इंडीकेट करता है, और फिर इसके प्रॉपर्टीज को रेफ़्रेन्स कर इंडीकेट करते हैं।

Binding this keyword in JavaScript function.

कभी-कभी, किसी फंक्शन प्रोग्राम में यह तय करते है, जहा आपको this कीवर्ड के वैल्यू को बाइंड करना पड़ सकता है। तो यहाँ प्रोग्रामर bind() मेथड का उपयोग करके ऐसा कर सकते हैं।

Binding this of a JavaScript function to a specific object.

let employee = {

  empname: “David”,

  empinfo() {

    console.log(“Hi, employee name is – ” + this.empname);

  }

};

let dispFn = employee.empinfo;

let boundinfo = dispFn.bind(employee);  // here Binding ‘this’ keyword used to the ’employee’ object

boundinfo();  // Result – Hi, employee name is – David

Explanation of Binding this keyword in JavaScript function.

इस प्रोग्राम में bind() फंक्शन मेथड, this कीवर्ड वैल्यू को employee ऑब्जेक्ट पर सेट करके एक नया फ़ंक्शन क्रिएट करता है, जब हम boundinfo() फंक्शन को कॉल करते हैं, तो this, कीवर्ड employee ऑब्जेक्ट प्रॉपर्टीज को रेफ़्रेन्स करता है।

Binding this keyword in callback function in JavaScript function.

जावास्क्रिप्ट प्रोग्राम में ऑब्जेक्ट मेथड्स को कॉलबैक के रूप में पास करते समय, this, ऑब्जेक्ट को रेफ़्रेन्स नहीं कर सकता है। यहाँ this कीवर्ड का सही वैल्यू को तय करने के लिए, आप bind() फंक्शन मेथड का उपयोग कर सकते हैं।

Example of binding this in callback function in function.

let employee = {

  empname: “Jack”,

  empinfo() {

    console.log(“Hi, employee name is – ” + this.empname);

  }

};

setTimeout(employee.empinfo, 3000);  // Result – Hi, employee name is – undefined because ‘this’ is not bound

Explanation of bind this in callback function in function.

यहाँ bind() मेथड यह तय करती है कि empinfo के अंदर this, employee ऑब्जेक्ट को रेफ़्रेन्स करता है, भले ही इस मेथड का उपयोग कॉलबैक के रूप में किया जाता हो।

Summary of this keyword in JavaScript function objects.

  • Inside object methods – फंक्शन में this कीवर्ड उस ऑब्जेक्ट के रेफ़्रेन्स को इंडीकेट करता है, जिस ऑब्जेक्ट का वह हिस्सा होता है।
  • Global context – this कीवर्ड ग्लोबल ऑब्जेक्ट ब्राउज़र में विंडो के रेफ़्रेन्स को इंडीकेट करता है।
  • Regular functions – this कीवर्ड इस पर डिपेंड करता है कि फ़ंक्शन को ऑब्जेक्ट में कैसे कॉल किया जाता है।
  • Arrow functions – फंक्शन प्रोग्राम में this लिट्रली रूप से आसपास के रेफ़्रेन्स से बंधा होता है, ऑब्जेक्ट से नहीं होता है।
  • Constructor functions/classes – फंक्शन प्रोग्राम में this कीवर्ड से बनाए जा रहे ऑब्जेक्ट के इंस्टेंस के रेफ़्रेन्स को इंडीकेट करता है।
  • Binding this – फ़ंक्शन या कॉलबैक में this कीवर्ड का वैल्यू स्पष्ट रूप से सेट करने के लिए bind() मेथड का उपयोग कर सकते है।

Leave a Reply