2012-10-31 9 views
5

के भीतर "इस" कीवर्ड की टाइपस्क्रिप्ट व्याख्या। मुझे एहसास है कि टाइपस्क्रिप्ट कंपाइलर सादे पुरानी जावास्क्रिप्ट को सच रहने का प्रयास कर रहा है, क्योंकि टाइपस्क्रिप्ट वास्तव में जावास्क्रिप्ट है। हालांकि, इंटेलिसेंस ने "यह" कीवर्ड और रनटाइम के दौरान वास्तव में क्या हल किया है, इसके बीच एक डिस्कनेक्ट है।

getAgencies() { 
      var self = this;   
      $.ajax(liveString + "/Home/GetSupportedAgencies", 
      { 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       error: Utilities.Logger.displayAjaxError, 
       success: this.onGetAgenciesComplete 
      }); 

     } 

और उसके संगत कॉलबैक: उदाहरण के लिए, निम्नलिखित टाइपप्रति ajax कॉल पर विचार

onGetAgenciesComplete(agencies) { 
       var self = this; 
        if (agencies == null) 
         Utilities.Logger.displayErrorOnLogConsole("There was an error retrieving supported agencies. Refresh site and try again."); 
        else { 
         $.each(agencies, function (i, a) { 
          self._indexViewModel.agencies.push({ name: a.Name, fullName: a.FullName, shortName: a.ShortName, bbox: a.BBox, countryCode: a.CountryCode }); 
         }); 

         if (Modernizr.geolocation) { 
          navigator.geolocation.getCurrentPosition(
           function (position) { 
            self.initMapPage(position, self); 
           }, 
           function (error) { 
            Utilities.Logger.displayErrorOnLogConsole("Oops, we could not get your location at his time. Please try later."); 
           }); 
         } 
         else { 
          Utilities.Logger.displayErrorOnLogConsole("Sorry, your browser does not return location information."); 
          self.getBusRoutes(self.agencyName); 
         } 


         // end of initialization 
        } 
       } 

अब, जब मैं लाइन पर ले जाएं, "var स्वयं = इस" टाइपप्रति स्रोत फ़ाइल के भीतर onGetAgenciesComplete में, वेरिएबल "सेल्फ" की इंटेलिसेंस परिभाषा इंगित करती है कि यह होमपेज व्यू मॉडेलबेस है, जहां होमपेज व्यू मॉडेलबेस वह वर्ग है जिसमें उपर्युक्त विधियां हैं।

HomePageViewModelBase.prototype.getAgencies = function() { 
      var self = this; 
      $.ajax(liveString + "/Home/GetSupportedAgencies", { 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       error: Utilities.Logger.displayAjaxError, 
       success: this.onGetAgenciesComplete 
      }); 
     }; 
     HomePageViewModelBase.prototype.onGetAgenciesComplete = function (agencies) { 
      var self = this; 
      if(agencies == null) { 
       Utilities.Logger.displayErrorOnLogConsole("There was an error retrieving supported agencies. Refresh site and try again."); 
      } else { 
       $.each(agencies, function (i, a) { 
        self._indexViewModel.agencies.push({ 
         name: a.Name, 
         fullName: a.FullName, 
         shortName: a.ShortName, 
         bbox: a.BBox, 
         countryCode: a.CountryCode 
        }); 
       }); 
       if(Modernizr.geolocation) { 
        navigator.geolocation.getCurrentPosition(function (position) { 
         self.initMapPage(position, self); 
        }, function (error) { 
         Utilities.Logger.displayErrorOnLogConsole("Oops, we could not get your location at his time. Please try later."); 
        }); 
       } else { 
        Utilities.Logger.displayErrorOnLogConsole("Sorry, your browser does not return location information."); 
        self.getBusRoutes(self.agencyName); 
       } 
      } 
     }; 

जो जब HomePageViewModelBase.prototype.onGetAgenciesComplete में चर "स्व" मार डाला क्या एक AjaxContext तरह लग रहा है और नहीं HomePageViewModelBase का एक उदाहरण के लिए हल हो गई है:

ऊपर उल्लिखित के लिए उत्पन्न जावास्क्रिप्ट इस प्रकार है। क्या यह अपेक्षित व्यवहार है या क्या मुझे इसे एक बग के रूप में रिपोर्ट करना चाहिए?

उत्तर

6

हां आपको शायद उन्हें एक बग के रूप में रिपोर्ट करना चाहिए, क्योंकि $.ajax() के भीतर $.ajax() ऑब्जेक्ट को संदर्भित करने के लिए है।

आप इसे चारों ओर जाने के लिए इतना है कि यह काम करता है चाहते हैं, के लिए अपनी सफलता समारोह बदलने के लिए:

success: self.onGetAgenciesComplete 

या यदि आप चाहते this अपनी कक्षा के लिए खड़े करने के लिए, बस संदर्भ विधि का उपयोग $ .ajax

$.ajax({ 
    context: this, 
    // now *this* will come from the previous scope, 
    // which in your case, is your class "HomePageViewModelBase" 

    // now that we know for certain what -this- refers to 
    success: this.onGetAgenciesComplete 
}); 
+0

अच्छा जवाब - संदर्भ की स्थापना काम करता है। हालांकि, आप एक बग सबमिट करने से पहले, इस प्रश्न पर विचार करें: संकलक कई अलग-अलग संदर्भों में फ़ंक्शन कहलाए जाने पर सही ढंग से 'इस' के संदर्भ का अनुमान कैसे लगा सकता है। मेरा मानना ​​है कि यह कहीं और से कॉलबैक के संदर्भ के बजाय, सीधे कार्य कहलाता है, इस धारणा के आधार पर सही संदर्भ दिखाता है। – Fenton

+0

मैं सहमत हूं, और यदि कुछ भी अच्छा अभ्यास है (यदि कुछ भी पठनीयता के लिए भी है) यह स्पष्ट करने के लिए कि 'इस' का संदर्भ यहां होना चाहिए। मुझे लगता है कि @ केएनजी का मुद्दा था, क्या यह नहीं पता होना चाहिए कि यह '$ .ajax' है, क्योंकि यह jQuery के भीतर काम करता है? –

+0

@mcpDESIGNS, यह एक अच्छा स्पष्टीकरण है, धन्यवाद। मैं $ .ajax संदर्भ के बारे में कभी नहीं जानता था जो एक बेहतर समाधान है। डबल धन्यवादआप मेरी पोस्ट के बिंदु में भी सही हैं, जो इंटेलिजेंस को होमपेज व्यू मॉडेल बेस के उदाहरण के लिए $ .ajax में "इस" में प्रस्तुत नहीं किया जाना चाहिए था। –

3

जहां तक ​​किसी भी (सहित Intellisense) स्थिर विश्लेषण का संबंध है, onGetAgenciesCompleteके संदर्भ से मैं एसHomePageViewModelBase और हमेशा होगा; सिवाय इसके कि रनटाइम पर, स्पष्ट आंतरिक jQuery.ajax संदर्भ बाध्यकारी के माध्यम से संदर्भ गतिशील रूप से सेट किया जाएगा। इंटेलिसेंस एकमात्र तरीका यह निर्धारित कर सकता है कि संदर्भ गतिशील रूप से उस कोड पथ को निष्पादित करने के लिए गतिशील रूप से बदल जाएगा, लेकिन इसके परिणामस्वरूप अस्पष्टता भी होती है: कौन सा संदर्भ सही संदर्भ है?

क्या होगा यदि आप एक-एक कॉल के लिए कहीं और "उधार" लेते हैं? क्या Intellisense संदर्भ को निर्धारित करने के लिए उस कॉल साइट का उपयोग करना चाहिए? शायद नहीं ...

HomePage.prototype.onGetAgencies.call({ some other context }, ...); 

(पठनीयता के लिए छोटा।)

संबंधित मुद्दे