2013-08-28 56 views
0

我無法感謝您的足夠時間和幫助!我已經搜索了近2天,無法找到我確切的答案。開始:Javascript子方法/帶構造函數的命名空間

我一直使用對象文字符號來創建我的對象。不過,我最近遇到了需要創建同一對象的多個實例的情況。我相信我試圖創建的「構造函數」:

我需要創建多個「窗口」對象的能力:

var window1 = new Window(); 
var window2 = new Window(); 
var window3 = new Window(); 

我希望能夠組織方法的能力作爲這樣的:

window1.errorMessage.show(); 
window2.errorMessage.hide(); 
window3.errorMessage.hide(); 

,而不是像這樣:

window1.showErrorMessage(); 
window2.hideErrorMessage(); 
window3.hideErrorMessage(); 

我將如何建立我的窗口對象中的文字符號的一個例子:

var Window = { 
    id: null, 

    init : function(id) { 
     this.id = id; 
    }, 

    errorMessage : { 
     show : function(message) { 
      // jquery that will simply take the id of this window, 
      // find the errorMessage html element within the window, 
      // insert the "message" and show it. 
     }, 

     hide : function() { 
      // jquery that will simply take the id of this window, 
      // find the errorMessage html element within this window and hide it. 
     } 
    } 
} 

的我怎麼會嘗試使用構造函數和原型建立我的窗口對象的例子:

function Window(id) { 
    this.id = id; 

    this.errorMessage = function() {} 
} 

Window.prototype.errorMessage = function() {} 

Window.errorMessage.prototype.show = function(message) { 
    // jquery that will simply take the id of this window, 
    // find the errorMessage html element within the window, 
    // insert the "message" and show it. 
} 

Window.errorMessage.prototype.hide = function() { 
    // jquery that will simply take the id of this window, 
    // find the errorMessage html element within this window and hide it. 
} 

當我嘗試執行以下代碼:

var window1 = new Window(); 

window1.errorMessage.show('An error message'); 

(最後,我想用叫它:)

this.errorMessage.show('An error message'); 

我收到以下錯誤控制檯從Firefox:

  • 類型錯誤:Window.errorMessage未定義
  • 類型錯誤:Window.errorMessage .show不是函數




非常感謝您的幫助。我很感激。

+1

正如一個建議,不要使用'Window' - 它已經是一個接口:https://developer.mozilla.org/en-US/docs/Web/API/Window – Ian

+1

您分配屬性'errorMessage'到'Window.prototype',但是然後試圖訪問'Window.errorMessage',它不存在。 –

+0

哦,我絕對不會在最後使用「窗口」。我只是試圖簡化我試圖實現的目標。但是,謝謝:) – MSchwartz

回答

2

我仍然會宣佈你對函數的原型方法,如你嘗試,但你必須要在一個新的ErrorMessage類型聲明showhide方法。我認爲像這樣是最好的和最有效的(因爲ErrorMessage的實例都將共享相同的showhide方法)要做的事情(如果我正確理解您的需求)。

function Window(id) { 
    this.id = id; 
    this.errorMessage = new ErrorMessage(); 
} 

function ErrorMessage() { } 
ErrorMessage.prototype.show = function() {} 
ErrorMessage.prototype.hide = function() {} 
2

如果您正在進行繼承,則只需要使用prototype。由於你現在沒有繼承遺忘關於prototype

每個Window都有一個ErrorMessage的實例。所以,我會寫它想:

function Window(id) { 
    this.id = id; 
    this.errorMessage = new ErrorMessage(); 
} 
function ErrorMessage() { 
    this.show = function() {}; 
    this.hide = function() {}; 
} 
var window1 = new Window(); 
window1.errorMessage.show(); 
+2

'原型'不僅僅是爲了繼承。它可以完全如何使用OP,只是做了一些修改。使用它來設置要由所有實例共享的屬性沒有任何問題。儘管我非常喜歡你的解決方案,但它爲每個實例創建了一個新的「ErrorMessage」,這可以通過共享來避免。 – Ian

+0

這聽起來像他會想要爲每個窗口不同的錯誤消息,所以爲每個窗口創建一個新的實例是正確的。 –

+2

@Frits像Ian說的那樣,在原型上聲明方法還有其他很好的理由。將方法聲明爲原型的屬性比每次實例化對象時創建新方法的性能高得多。 –