PDA

View Full Version : "Public" and "Private" Fields



DarthMaul
28 Apr 2009, 1:25 PM
I am creating an extension of panel with a series of configurations, and I want this subclass to have public and private methods. Here is a snippet:



Ext.namespace('MyWidget');

MyWidget.MainPanel = function(config) {
var defaultMargins = '5 5 5 5';
var gridStore = new Ext.data.Store({
url : 'data/myWidget.xml',
reader : new Ext.data.XmlReader({id : 'Id',record : 'Record'}, [ 'Name', 'Id' ])
});
var checkboxSelectionModel = new Ext.grid.CheckboxSelectionModel();
.
.
.
function launchWindow()
{
Ext.Msg.alert("launch");
}
.
.
.
Ext.apply(mainPanelConfig, config);
Ext.apply(this, mainPanelConfig);

gridStore.load();

MyWidget.MainPanel.superclass.constructor.call(this);
}

var myWidget = Ext.extend(MyWidget.MainPanel,Ext.Panel);
My understanding is that the properties and methods defined as above inside MyWidget.MainPanel are all "private." I want to expose properties and methods as public so that a caller or a JSUnit test can call MyWidget.MainPanel.foo() or access MyWidget.MainPanel.gridStore.

Any insight into this is much appreciated.

Thanks.

mmusson
28 Apr 2009, 7:11 PM
The short answer is make an assignment like this.gridStore = gridStore in the constructor. Any properties you set on this will be public. But if you are going to make these properties public, why bother making them private to begin with?

evant
28 Apr 2009, 7:14 PM
It's the same issue with any other language, if you want to unit test private members you have to do something out of the ordinary to access them. That being said, making members fully private takes a little effort in JS, Ext, for example just marks private members with a comment, as opposed to enforcing their access level.