2014-10-03 64 views
0

我要開始構建好我的JavaScript,使我在這個代碼審查描述我沒有污染全局命名空間以及更好的做法封裝和繼承:https://codereview.stackexchange.com/q/64556/42628JavaScript,如何在使用單例時保持適當的封裝?

所以,借這個例子(here is a fiddle of it)。 ..

window.myPage = { 
    init:function(){ 
     //local "Private" declarations 
     var gridHandle1 = new myPage.MyGrid(); 
     gridHandle1.loadGridData(); 
    }, 
    MyGrid: function(){ 
     /* local "Private" declarations */ 
     var dataLoadTimes = 0; 
     var gridDrawn = true; 
     $('#debug').append('grid is drawn<br>'); 

     //This needs to be a public method because 
     //other functions need to reload the grid 
     //at various times. So I will use "this." 
     //instead of "var". "var" would make it a 
     //private method. 
     this.loadGridData = function() { 
      dataLoadTimes = dataLoadTimes+1; 
      $('#debug').append('grid data has been loaded '+dataLoadTimes+' times<br>'); 
     }; 

     this.loadGridData(); 
    } 
}; 

myPage.init(); 
// console.log(gridHandle1); // <--- fails, GOOD, it's not in the global namespace 
// console.log(gridDrawn); // <--- fails, GOOD, it's not in the global namespace 

我明白了一切會在這裏,我喜歡它,因爲「loadGridData」僅適用於「MyGrid」,它有沒有業務「MyGrid」之外它自己的功能,這會破壞封裝。但做這種方式需要這條線......

var gridHandle1 = new myPage.MyGrid();

這很酷,它讓我創造了許多網格這樣的...

var gridHandle1 = new myPage.MyGrid(); 
var gridHandle2 = new myPage.MyGrid(); 
var gridHandle3 = new myPage.MyGrid(); 

但在這種情況下我不需要很多網格。我只需要一個。所以我的問題是,我將如何調整此代碼,以便一切工作相同,但將它作爲單例使用,而不是使用「類」方法並使用構造函數實例化對象?

回答

0

有幾種方法可以實現這一目標,並最終達到最佳效果。

我個人這樣的代碼:(不jQuery的 - 我不使用它)

var myPage = { 
    ua: {} 
} 

$(function() { 
    myPage.mainLayout = new dhtmlXLayoutObject(document.body, "2U"); 
    myPage.mainTabBar = myPage.mainLayout.cells("b").attachTabbar(); 
    myPage.initTab(myPage.mainTabBar); 
}); 


myPage.initTab=function(tb){ 
    tb.xyz(); 
}: 


myPage.ua.load=function(id){ 
    myPage.ua.innerLayout = myPage.mainTabBar.tabs(id).attachLayout("1C"); 
    myPage.ua.grid  = myPage.ua.innerLayout.cells("a").attachGrid(); 
} 

myPage.ua.search = function() { 
    myPage.ua.grid.clearAndLoad([theUrl], "json"); 
}; 

有時候我定義ua,有些時候我把一切都綁myPgae

+0

謝謝。我完全重新提出了我的問題,希望它更清楚。我不想按照你的建議去做,因爲它破壞了封裝。 Search()應該是它的網格對象的成員函數,而不是一個獨立的函數。網格不是var scoped,所以會出血,以便search()可以訪問它等等。希望我更新後的問題能夠讓我更清楚地知道我在找什麼。 – gfrobenius 2014-11-20 22:21:30

+0

你會發現試圖在不創建對象的情況下封裝變量和函數是不值得的(在javascirpt中)。有很多關於它的書面文章,它歸結爲:如果用戶可以創建一個變量,他們幾乎可以調用任何函數,重新定義對象並設置其中包含的變量。你會發現自己正沿着定義閉包的路線,該閉包消耗的內存比用原型構建對象消耗更多的內存(這是做上面寫的正確方法)。因爲你不想使用物體,底線:這是一種痛苦,不值得。 – 2014-11-21 00:19:42

+0

考慮一下你所說的:「Search()應該是一個成員函數」讓成員擁有一個對象的唯一方法。 – 2014-11-21 00:55:53

相關問題