एक जावास्क्रिप्ट फ़ाइल में मैंने देखा:
function Somefunction(){
var that = this;
...
}
इसे घोषित करने thatऔर नियत करने का उद्देश्य क्या है this?
एक जावास्क्रिप्ट फ़ाइल में मैंने देखा:
function Somefunction(){
var that = this;
...
}
इसे घोषित करने thatऔर नियत करने का उद्देश्य क्या है this?
जवाबों:
मैं इस उत्तर को दृष्टांत के साथ शुरू करने जा रहा हूं:
var colours = ['red', 'green', 'blue'];
document.getElementById('element').addEventListener('click', function() {
// this is a reference to the element clicked on
var that = this;
colours.forEach(function() {
// this is undefined
// that is a reference to the element clicked on
});
});
मेरे उत्तर ने मूल रूप से jQuery के साथ इसका प्रदर्शन किया, जो केवल थोड़ा अलग है:
$('#element').click(function(){
// this is a reference to the element clicked on
var that = this;
$('.elements').each(function(){
// this is a reference to the current element in the loop
// that is still a reference to the element clicked on
});
});
क्योंकि thisअक्सर जब आप किसी नए फ़ंक्शन को कॉल करके गुंजाइश बदलते हैं, तो आप इसका उपयोग करके मूल मूल्य तक नहीं पहुंच सकते। इसे एलियास करने से thatआप मूल मूल्य तक पहुँच सकते हैं this।
व्यक्तिगत रूप से, मैं thatउर्फ के रूप में उपयोग को नापसंद करता हूं । यह शायद ही कभी स्पष्ट है कि यह किसका उल्लेख कर रहा है, खासकर यदि फ़ंक्शन कुछ पंक्तियों से अधिक लंबा हो। मैं हमेशा अधिक वर्णनात्मक उपनाम का उपयोग करता हूं । उपरोक्त मेरे उदाहरणों में, मैं शायद उपयोग करूँगा clickedEl।
var self = this;। शब्द thatका अर्थ लगता है कि परिवर्तनशील कुछ भी है this।
forEachसमारोह में एक दूसरे वैकल्पिक तर्क है जो समारोह के बाध्यकारी है लेता है। colours.forEach(function(){/* 'this' is bound correctly --> */}, this);तो एक नोट जोड़ा जाना चाहिए जो वास्तव में आवश्यक var that = thisनहीं है । forEach
से Crockford
परंपरा के अनुसार, हम एक निजी बनाने कि चर। इसका उपयोग वस्तु को निजी तरीकों को उपलब्ध कराने के लिए किया जाता है। यह ECMAScript लैंग्वेज स्पेसिफिकेशन में त्रुटि के लिए एक वर्कअराउंड है, जो इसे आंतरिक कार्यों के लिए गलत तरीके से सेट करता है।
function usesThis(name) {
this.myName = name;
function returnMe() {
return this; //scope is lost because of the inner function
}
return {
returnMe : returnMe
}
}
function usesThat(name) {
var that = this;
this.myName = name;
function returnMe() {
return that; //scope is baked in with 'that' to the "class"
}
return {
returnMe : returnMe
}
}
var usesthat = new usesThat('Dave');
var usesthis = new usesThis('John');
alert("UsesThat thinks it's called " + usesthat.returnMe().myName + '\r\n' +
"UsesThis thinks it's called " + usesthis.returnMe().myName);
यह अलर्ट ...
UseThat को लगता है कि इसे डेव कहा जाता है
उपयोग यह सोचता है कि इसे अपरिभाषित कहा जाता है
thatचर का उपयोग उनके उदाहरण में बिल्कुल नहीं किया गया है। यह ऐसा लगता है जैसे कि बस एक चर होल्डिंग बनाना thisबाकी कोड के लिए कुछ करता है।
यह आंतरिक कार्यों (अन्य कार्यों के अंदर परिभाषित कार्य) को बनाने के लिए एक हैक है, जैसे उन्हें और अधिक काम करना चाहिए। जावास्क्रिप्ट में जब आप एक फ़ंक्शन को दूसरे के अंदर परिभाषित करते हैं तो thisस्वचालित रूप से वैश्विक दायरे में सेट हो जाता है। यह भ्रामक हो सकता है क्योंकि आपको thisबाहरी फ़ंक्शन के समान मूल्य की उम्मीद है।
var car = {};
car.starter = {};
car.start = function(){
var that = this;
// you can access car.starter inside this method with 'this'
this.starter.active = false;
var activateStarter = function(){
// 'this' now points to the global scope
// 'this.starter' is undefined, so we use 'that' instead.
that.starter.active = true;
// you could also use car.starter, but using 'that' gives
// us more consistency and flexibility
};
activateStarter();
};
यह विशेष रूप से एक समस्या है जब आप किसी फ़ंक्शन को किसी ऑब्जेक्ट की विधि के रूप में बनाते हैं ( car.startउदाहरण में) तो उस विधि (जैसे activateStarter) के अंदर एक फ़ंक्शन बनाएं । शीर्ष स्तर की विधि thisऑब्जेक्ट को इंगित करती है यह (इस मामले में car) की एक विधि है, लेकिन आंतरिक फ़ंक्शन मेंthis अब वैश्विक गुंजाइश की ओर इशारा करता है। यह एक दर्द है।
दोनों स्कैप में कन्वेंशन द्वारा उपयोग करने के लिए एक वैरिएबल बनाना, जावास्क्रिप्ट के साथ इस सामान्य समस्या के लिए एक समाधान है (हालांकि यह jquery कार्यों में उपयोगी है, भी)। यही कारण है कि बहुत सामान्य लग नाम thatका उपयोग किया जाता है। यह भाषा में एक कमी पर काबू पाने के लिए एक आसानी से पहचाने जाने योग्य सम्मेलन है।
पर जैसा एल Ronnoco संकेत डगलस Crockford सोचता है कि यह एक अच्छा विचार है।
के उपयोग thatवास्तव में जरूरी है अगर आप के उपयोग के साथ एक समाधान नहीं है call()या apply():
var car = {};
car.starter = {};
car.start = function(){
this.starter.active = false;
var activateStarter = function(){
// 'this' now points to our main object
this.starter.active = true;
};
activateStarter.apply(this);
};
कभी-कभी thisएक और गुंजाइश का उल्लेख कर सकते हैं और कुछ और का उल्लेख कर सकते हैं, उदाहरण के लिए मान लें कि आप एक DOM इवेंट के अंदर एक कंस्ट्रक्टर विधि को कॉल करना चाहते हैं, इस मामले मेंthis में DOM तत्व का संदर्भ होगा न कि बनाई गई वस्तु का।
एचटीएमएल
<button id="button">Alert Name</button>
जे एस
var Person = function(name) {
this.name = name;
var that = this;
this.sayHi = function() {
alert(that.name);
};
};
var ahmad = new Person('Ahmad');
var element = document.getElementById('button');
element.addEventListener('click', ahmad.sayHi); // => Ahmad
इच्छा से ऊपर समाधान assing thisके thatअंदर तो हम कर सकते हैं और उन तक पहुंचने नाम संपत्ति sayHiसे विधिthat , यह इतना डोम कॉल अंदर मुद्दों के बिना कहा जा सकता है।
एक अन्य उपाय यह है कि आप किसी खाली thatवस्तु को असाइन करें और उसमें गुण और विधियाँ जोड़ें और फिर उसे वापस लौटाएँ। लेकिन इस समाधान के साथ आप prototypeकंस्ट्रक्टर के खो गए।
var Person = function(name) {
var that = {};
that.name = name;
that.sayHi = function() {
alert(that.name);
};
return that;
};
यहाँ एक उदाहरण है `
$(document).ready(function() {
var lastItem = null;
$(".our-work-group > p > a").click(function(e) {
e.preventDefault();
var item = $(this).html(); //Here value of "this" is ".our-work-group > p > a"
if (item == lastItem) {
lastItem = null;
$('.our-work-single-page').show();
} else {
lastItem = item;
$('.our-work-single-page').each(function() {
var imgAlt = $(this).find('img').attr('alt'); //Here value of "this" is '.our-work-single-page'.
if (imgAlt != item) {
$(this).hide();
} else {
$(this).show();
}
});
}
});
});`
तो आप देख सकते हैं कि आपके द्वारा लक्षित DOM तत्व के आधार पर इसका मूल्य दो अलग-अलग मान हैं लेकिन जब आप "उस" को ऊपर दिए गए कोड में जोड़ते हैं तो "आप" जो आप लक्ष्य कर रहे हैं उसका मान बदल जाता है।
`$(document).ready(function() {
var lastItem = null;
$(".our-work-group > p > a").click(function(e) {
e.preventDefault();
var item = $(this).html(); //Here value of "this" is ".our-work-group > p > a"
if (item == lastItem) {
lastItem = null;
var that = this;
$('.our-work-single-page').show();
} else {
lastItem = item;
$('.our-work-single-page').each(function() {
***$(that).css("background-color", "#ffe700");*** //Here value of "that" is ".our-work-group > p > a"....
var imgAlt = $(this).find('img').attr('alt');
if (imgAlt != item) {
$(this).hide();
} else {
$(this).show();
}
});
}
});
});`
..... $ (वह) .css ("पृष्ठभूमि-रंग", "# ffe700"); // यहाँ "उस" का मान है। हमारा-कार्य-समूह> p> a "क्योंकि var का मान = यह; भले ही हम "इस" = '.our-work-single-page' पर हैं, फिर भी हम पिछले DOM तत्व में हेरफेर करने के लिए "उस" का उपयोग कर सकते हैं।