2014-06-28 38 views
0

這是我的情況。這個裏面的回調函數

function Bird() { 
    this._canFly = true; 
    this._legs = 2; 
    this._flying = false; 
} 

Bird.prototype = { 
    Fly: function() { 
    if (this.canFly) { 
     layer.on('fly', function() { 
      this.setStrokeWidth(4); //this refers to layer(kinetic.js) object 
      this._flying = true; //this refers to Bird object 
     }); 
    }//end if 
    } //end function 
); 

這裏我需要訪問回調函數中的圖層對象和鳥對象。 有人可以告訴我如何處理上述情況?

回答

1
var self = this 

緩存對this的引用,以在其更改上下文時引用它。