2013-09-28 36 views
0

鑑於以下JS代碼:使用OOJS的遞歸方法

+ -------------------------------- ------------- +

this.Element = function() { 

this.twitch = function(e) { 
     $(e).animate({ 
      height: "+=5" 
     }, 1000, function() { 
      $(e).animate({ 
       height: "-=5" 
      }, 1000, function() { 
      }); 
     }); 
    }; 

$(document).ready(function() { 
    var footer = new this.Element(); 
    footer.twitch("#footer"); 
}); 

+ ---------------------------- ----------------- +

我怎樣才能調用方法「twitch()」遞歸?

謝謝。

回答

0

第一個錯誤,this是指的jQuery的在這方面new this.Element();

修正了:

this.Element = function(return this;) { 

this.Element.prototype.twitch = function(e) { 
var self = this; 
     $(e).animate({ 
      height: "+=5" 
     }, 1000, function() { 
      $(e).animate({ 
       height: "-=5" 
      }, 1000, function() { 
       self.twitch(e); 
      }); 
     }); 
    }; 

var self = this; 
$(document).ready(function() { 
    var footer = new this.Element(); 
    footer.twitch("#footer"); 
});