2012-01-27 67 views
0

我有以下代碼。在下面的代碼中,有兩個函數調用另一個函數。但是我不明白爲什麼一旦這個函數被調用,而另一次這個函數被另一個地方的變量調用。爲什麼要使用「this」來調用jquery中的函數

var widgetMethods = { 

    getWidgetData: function($widgetElement) { 

      var widgetData = $widgetElement.data('widgetData'); 

      widgetData = (typeof widgetData == 'undefined') ? { type: null, key: null } : widgetData; 

      if(widgetData.type == null) { 
      console.log("Widget type is not specified!"); 
      return false; 
      } 

      if(widgetData.key == null) { 
      console.log("Widget key is not specified!"); 
      return false; 

      } 
      return widgetData; 
     }, 

    editWidget: function(key, options) { 

      var $self = jQuery(this); 
      var widgetData = widgetMethods.getWidgetData($self); 
      } 

    getWidgetTemplate: function($widgetElement) { 

      var widgetData = this.getWidgetData($widgetElement); 
      } 


    } 

有人可以幫助我。我很困惑。請簡要說明。

回答

0

這是指調用該函數的元素的當前指針。因此,您可以使用「this」傳遞當前元素。然而,當你調用一個不是由同一個元素引用的函數時,你需要傳遞相同的屬性來使用它。

。如果你是疑惑地看着這個http://msdn.microsoft.com/en-us/library/y0dddwwd.aspx

:)

+0

但爲什麼我不能調用editWidget的getWidgetData此對象 – user1172928 2012-01-27 07:26:14

+0

不使用$自...使用VAR自我= $(這個) 然後, var widgetDataMethods.getWidgetData(self); – sree 2012-01-27 10:39:19

相關問題