簡體   English   中英

javascript從flot綁定函數訪問類對象

[英]javascript accessing class objects from flot bound function

我有一個javascript“類”,它有兩種方法。 在一種方法中,我試圖創建一個flot plothover綁定,該綁定可以訪問進行綁定的類的屬性和方法。 我要弄清楚的是如何從綁定函數中訪問類的屬性和方法。

var MyClass = 
{
  Property1 = null,
  ShowToolTip: function( x, y, text ) { ...stuff... },
  Render: function ( arg1, arg2 ) 
  {
     this.Property1 = "this works";
     $('#placeholder').bind('plothover', function (event, pos, item ) {
        this.Property1 = "non workie";         // need access to Property1
        this.ShowToolTip( 10, 10, "stuff" );   // need access to ShowToolTip
     }
  }
}

顯然,我不能使用'this'來查看MyClass-那么可以從bind函數中訪問和調用MyClass的屬性和方法嗎?

我可能有多個MyClass的克隆,所以我需要做的事情必須在每個克隆的類中隔離。

感謝您的任何建議。 科里

您可以創建this的引用:

Render: function ( arg1, arg2 ) 
      {
         this.Property1 = "this works";
         var that = this;
         $('#placeholder').bind('plothover', function (event, pos, item ) {
            that.Property1 = "non workie";         // need access to Property1
            that.ShowToolTip( 10, 10, "stuff" );   // need access to ShowToolTip
         }
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM