Re: Replacing a private function an keeping access to private variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas 'PointedEars' Lahn

    Re: Replacing a private function an keeping access to private variables

    Gregor Kofler wrote:
    Code:
    function foo() {
       var privateVar = 42;
    >
       var privateFunc = function() {
         alert(privateVar);
       };
    >
       return {
         replacePF: function(f) { privateFunc = f; },
         accessPF: function() { privateFunc(); }
       };
    }
    >
    var obj = foo();
    I am curious, why did you choose to use a factory instead of a constructor?
    Code:
    obj.accessPF(); // 42
    >
    obj.replacePF(function() {
       window.alert("The answer is "+privateVar);
    });
    obj.accessPF(); // privateVar is undefined
    It's a closure and `privateVar' in privateFunc() is bound to the execution
    context of foo(), in which `privateFunc' was declared a variable. However,
    the function expression in the obj.replacePF() call creates a closure in
    which `privateVar' is bound to the execution context in which the call is
    executed and the anonymous Function object is created, and in which scope
    chain no object has such a property.

    ISTM the only way to do this is to have a public method that returns
    `privateVar', which would become "protected" through this: Either

    Code:
    function foo()
    {
    var privateVar = 42;
    
    var privateFunc = function() {
    window.alert(privateVar);
    };
    
    return {
    getPrivateVar: function() { return privateVar; },
    replacePF: function(f) { privateFunc = f; },
    accessPF: function() { privateFunc(); }
    };
    }
    
    var obj = foo();
    or

    Code:
    function Foo()
    {
    var privateVar = 42;
    
    var privateFunc = function() {
    window.alert(privateVar);
    };
    
    this.getPrivateVar = function() { return privateVar; };
    this.replacePF = function(f) { privateFunc = f; };
    this.accessPF = function() { privateFunc(); };
    }
    
    var obj = new Foo();
    
    and then
    
    // 42
    obj.accessPF();
    
    obj.replacePF(function() {
    window.alert("The answer is " + obj.getPrivateVar());
    });
    
    // "The answer is 42"
    obj.accessPF();
    (It's the usual simple getter and setter implementation as it is.)


    HTH

    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
  • Gregor Kofler

    #2
    Re: Replacing a private function an keeping access to private variables

    Thomas 'PointedEars' Lahn meinte:
    I am curious, why did you choose to use a factory instead of a constructor?
    I've started switching from constructor instantiation to a factory like
    pattern after reading Crockfords new book, and realizing that I tend to
    have objects with a meager amount of public properties, but lots of
    private functions. On the other hand I do need a factory occassionally.
    Since I try to avoid mixing the patterns, I stick to factories most of
    the time now.
    It's a closure and `privateVar' in privateFunc() is bound to the execution
    context of foo(), in which `privateFunc' was declared a variable. However,
    the function expression in the obj.replacePF() call creates a closure in
    which `privateVar' is bound to the execution context in which the call is
    executed and the anonymous Function object is created, and in which scope
    chain no object has such a property.
    Yep, I know the reason, why I can't access it this way, but I was rather
    tired yesterday evening and had this feeling that missed something.
    ISTM the only way to do this is to have a public method that returns
    `privateVar', which would become "protected" through this
    [snip]

    Ok, that's the approach I already use.
    HTH
    Sort of.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum

    Comment

    Working...