Helma Logo
main list history

Private methods in modules

Normally all variables and methods in Helma-NG modules are "public", but there is a way to create private ones:

 (function(scope) {
    
    var privateMethod = function() {
       writeln("I'm private");
       return;
    };
      
    scope.publicMethod = function() {
       privateMethod();
       writeln("I'm public");
       return;
    };
    
 })(global);

Instead of passing the global object as argument, one can also use "this" inside the method to reference the module's scope (so "scope.publicMethod" becomes "this.publicMethod"), however imho it's quite confusing.

Comments

#1 by hannes at 2008/05/12 18:49

Actually, "global" is the per-thread scope - the one that acts as prototype of all module scopes within one request as shown in this diagram. If you want to install public properties in the module scope, you should use "this" instead of "global" as function argument. But of course it's legitmate to install stuff in the per-thread global object. The helma.simpleweb module does this to set the global res, req and session properties.

#2 by hannes at 2008/05/16 22:08

I just read the last sentence - why do you think using "this" is confusing? Actually, I prefer using "this" to "self" as it feels more natural, and I feel like settling on this variant:

 (function() {
    
    var privateMethod = function() {
       writeln("I'm private");
       return;
    };
      
    this.publicMethod = function() {
       privateMethod();
       writeln("I'm public");
       return;
    };
    
 }).call(this);