1. #1
    Sencha User
    Join Date
    Aug 2010
    Posts
    83
    Vote Rating
    3
    gabrielle is on a distinguished road

      1  

    Default private variables & privileged methods

    private variables & privileged methods


    Hello,
    what would be the cleanest way to define real private variables and privileged methods within a class, using the Ext framework for defining classes?
    Thanks!
    Gabrielle

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    35,704
    Vote Rating
    751
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Nothing is private in JS. I have seen many coding styles but I do this:

    For private things, I prefix with an underscore "_".
    Example: var _myVar = "Hello";

    For everything else, I use camelCase.
    Example: var myVar = "Hello";
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Aug 2010
    Posts
    83
    Vote Rating
    3
    gabrielle is on a distinguished road

      1  

    Default


    Thank you Mitchell.

    By private, I meant as presented by Douglas Crockford here. E.g.
    PHP Code:
    function Container(param) {

        function 
    dec() {
            if (
    secret 0) {
                
    secret -= 1;
                return 
    true;
            } else {
                return 
    false;
            }
        }

        
    this.member param;
        var 
    secret 3;
        var 
    that this;

        
    this.service = function () {
            if (
    dec()) {
                return 
    that.member;
            } else {
                return 
    null;
            }
        };

    In the example above, secret being private (i.e. non accessible outside Container) and service is privileged (has access to private vars and is accessible from outside by means of this.service());

    When looking at ext source code, one can notice stuff like:

    PHP Code:
    Ext.define('Ext.panel.Proxy', {
        
        
    constructor: function(panelconfig){
            ...
        },

        
    /**
         * @cfg {Boolean} ...
         */
        
    insertProxytrue,

        
    // private overrides
        
    setStatusExt.emptyFn,
        
    resetExt.emptyFn,
        
    updateExt.emptyFn,
        
    stopExt.emptyFn,
        
    syncExt.emptyFn
    and
    PHP Code:
        /**
         * @private
         * Returns a setter function to be placed on the owner model's prototype
         * @return {Function} The setter function
         */
        
    createSetter: function() { 
    However, this is not private as in my first example. So just wondering if best practice in ExtJS is not to have private (in Crockford' sense) and if so, why.
    Kind regards

  4. #4
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    796
    Vote Rating
    37
    westy is a jewel in the rough westy is a jewel in the rough westy is a jewel in the rough

      0  

    Default


    AFAIK you cannot have private or privileged data or methods when the class is defined as a JSON object.

    All the examples you'll see define classes as functions, and use closures to hide data.
    Suppose you could use a function that returns the JSON object, but then it doesn't fit into the Ext way of defining classes.

    It might be nice if there was a way, perhaps similar to the way statics have been done, by having a 'private' or 'privileged' member. I think it'd still be visible though, but you could probably throw errors if accessed by a caller that is not allowed access.
    Doesn't feel right though...

    I'd probably go with naming the members with an underscore prefix or something. Not really come to a decision as yet.

  5. #5
    Ext JS Premium Member
    Join Date
    Oct 2009
    Posts
    30
    Vote Rating
    3
    iesdeveloper is on a distinguished road

      0  

    Thumbs up


    Just define it in the config's constructor:

    PHP Code:
    Ext.define('Container', {
                            
    constructor : function(config) {
                                function 
    dec() {
                                    if (
    secret 0) {
                                        
    secret -= 1;
                                        return 
    true;
                                    } else {
                                        return 
    false;
                                    }
                                }

                                
    this.member config.param;
                                var 
    secret 3;
                                var 
    that this;

                                
    this.service = function() {
                                    if (
    dec()) {
                                        return 
    that.member;
                                    } else {
                                        return 
    null;
                                    }
                                };
                            }
                        }); 

  6. #6
    Ext JS Premium Member devtig's Avatar
    Join Date
    Jan 2010
    Location
    Rotterdam, The Netherlands
    Posts
    390
    Vote Rating
    13
    devtig will become famous soon enough

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Nothing is private in JS.
    Please explain yourself considering http://javascript.crockford.com/private.html

  7. #7
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    35,704
    Vote Rating
    751
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    JS source can easily be seen and executed.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  8. #8
    Ext JS Premium Member devtig's Avatar
    Join Date
    Jan 2010
    Location
    Rotterdam, The Netherlands
    Posts
    390
    Vote Rating
    13
    devtig will become famous soon enough

      0  

    Default


    Seen yes, but executed from outside?

    Code:
    Container=function() {
      alert('You can alert me.');
      
      secret=function() {
        alert('But you cannot alert me (from outside).');
      };
    };

  9. #9
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    35,704
    Vote Rating
    751
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    And then i can see your source and do this in console...

    Code:
    function secret() {
        alert('But you can't see me.')
      };
    }
    It is the best that a JS dev can do. Why do you need private functions? Personally never have needed and for frontend that can be viewed, dont see a reason for doing it. Everything i do is absyracted so everything is reused.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  10. #10
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    796
    Vote Rating
    37
    westy is a jewel in the rough westy is a jewel in the rough westy is a jewel in the rough

      0  

    Default


    Quote Originally Posted by iesdeveloper View Post
    Just define it in the config's constructor
    Ah, of course, looks good.

    Still won't look very Extish, but serves the goal if needed.

Similar Threads

  1. public private privileged
    By iamharrisun in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 11 Jun 2008, 7:04 PM
  2. Private variables in extended functions
    By JamesC in forum Ext 2.x: Help & Discussion
    Replies: 6
    Last Post: 26 May 2008, 12:18 PM
  3. private functions and variables in ext class
    By dolittle in forum Ext 2.x: Help & Discussion
    Replies: 4
    Last Post: 25 May 2008, 7:37 AM

Thread Participants: 4

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar