Results 1 to 3 of 3

Thread: "Public" and "Private" Fields

  1. #1

    Default "Public" and "Private" Fields

    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:

    Code:
    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.

  2. #2
    Ext User
    Join Date
    Nov 2007
    Location
    Centreville, VA
    Posts
    58

    Default

    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?

  3. #3
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258

    Default

    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.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •