2013-01-07 60 views
-2

當我寫book.main.method();我有錯誤:遺漏的類型錯誤:目標函數(){錯誤:遺漏的類型錯誤:對象功能

window.book = window.book|| {}; 

    book.control = function() { 
     var check_is_ready = 'check_is_ready'; 
     if(check_is_ready == 'check_is_ready') { 
      this.startbook = function() { 
       var bookMain = new book.main(); 
        bookMain.method(); 
      return; 
      }; 
     }; 
    }; 

$(function() { 
    var control = new book.control(); 
    control.startbook(); 
}); 

(function() { 
    book.main = function() {  
    var index =0; 
     this.method = function() { 
      index++; 
      if (index <= 500){ 
       book.main.method();// <-- this wrong 
       // the error which I get :Uncaught TypeError: Object function() { 
       alert (index); 
       } 
     }; 
    }; 
})(); 

我應該寫,而不是book.main.method();與超時錯誤調用它?

千恩萬謝

+1

我想這可能是你的代碼的改進此刻,那並不解決您的問題,但我怕。 http://pastebin.com/6pvmdBKp – Undefined

回答

0

如果我理解正確的主要問題代碼是:

(function() { 
    book.main = function() {  
    var index =0; 
     this.method = function() { 
      index++; 
      if (index <= 500){ 
       book.main.method();// <-- this wrong 
       // the error which I get :Uncaught TypeError: Object function() { 
       alert (index); 
      } 
     }; 
    }; 
})(); 

您正嘗試遞歸調用method()。執行遞歸調用的另一種方法是給函數表達式(methodFn)命名。請記住這個名字僅僅是函數體內有效:

(function() { 
    book.main = function() {  
    var index =0; 
     this.method = function methodFn() { 
      index++; 
      if (index <= 500){ 
       methodFn(); 
       alert (index); 
      } 
     }; 
    }; 
})(); 
+0

你真了不起, – user1954217

0

你混淆構造(book.main)和實例。

this.method = function() {爲您使用new book.main()而不是book.main獲得的實例添加了一個函數。

我不知道你的確切最終目標,但此時應更換

book.main.method();// <-- this wrong 

this.method();// <-- this works 

還要注意的是,如果你想看到的警報的增加值,您必須切換2行:

(function() { 
    book.main = function() {  
    var index =0; 
    this.method = function() { 
     index++; 
     if (index <= 2){ 
      console.log(index); // less painful with console.log than with alert 
      this.method(); 
     } 
    }; 
    }; 
})(); 
+0

我寫你的行之前,我得到了錯誤'未捕獲TypeError:對象[對象窗口]沒有方法'方法'' – user1954217

+0

它「爲我工作」。 –

+0

我能否向我發送我的所有代碼? – user1954217

相關問題