mardi 27 octobre 2015

Accessing class members from private function

Suppose I have a class, with some methods that I need to access from private functions within that class. Like so:

var A = function( )
{
    this.a = 0;

    var f = function( ){   // Private function
        this.a;   // Undefined
    };

    f( );
};

What would be the better way to do this? I tried to pass this to the function, but it's not practical if I must do it for many functions.

var A = function( )
{
    this.a = 0;

    var f = function( self ){
        self.a;
    };

    f( this );
};

Is there a better way to do this? Or is the design fundamentally flawed and I should consider other alternatives? Thanks!

Aucun commentaire:

Enregistrer un commentaire