Home > Net >  Private const in Class NodeJS
Private const in Class NodeJS

Time:10-30

Well comrades, i have a issue that keep me from sleeping, i have this function:

class iniparser {
constructor (path) {
    this.path = path;
}

#_getValue (data, param) {
    const iniparam = /^\s*([\w\.\-\_] )\s*=\s*(.*?)\s*$/;
    let value;

    data.toString().match(/.*?\n|. $/g).forEach(function(line) {
        if (iniparam.test(line) && line.match(iniparam)[1] === param) {
            value = line.match(iniparam)[2];
        }
    });

    return value;
}

and then some public method to work with and all of that... The problem is iniparam, i want make it private, becouse i have a _setValue method that need the constant, and i want to write good code, i check TC39 but even using tons and tons of examples i can get it working. Please some soul help me.

CodePudding user response:

My simple recommendation would be to just define iniparam before the class definition. Assuming this code is in a module, then variables at the module level are available for use anywhere in the module, but private beyond that (not available outside the module unless you export them).

This notion of declaring shared constants before a body of code that wants to use them is very common in Javascript modules. You could even say that the practice of import or require() is doing the same thing - initializing some module-level constants that any code in the module can use. It even can make things slightly more efficient for the interpreter because the constant is just created/initialized once when the module is initialized rather than every time some function is called (though if it's const, the interpreter might already optimize for that).

// declare shared constants
const iniparam = /^\s*([\w\.\-\_] )\s*=\s*(.*?)\s*$/;

class iniparser {

    constructor (path) {
        this.path = path;
    }
    
    #getValue (data, param) {
        let value;
    
        data.toString().match(/.*?\n|. $/g).forEach(function(line) {
            if (iniparam.test(line) && line.match(iniparam)[1] === param) {
                value = line.match(iniparam)[2];
            }
        });
    
        return value;
    }
}

Then, any code in this module will have access to the constant, but no code outside the module can access it.


Note, in addition to declaring iniparam as a shared constant, you could also make getValue() itself a module level utility function. Since it doesn't seem to access instance data, it doesn't really need to be a method. If setValue() also doesn't access instance data, it could also be a module-level utility function.

// declare shared constants
const iniparam = /^\s*([\w\.\-\_] )\s*=\s*(.*?)\s*$/;

function getValue (data, param) {
    let value;

    data.toString().match(/.*?\n|. $/g).forEach(function(line) {
        if (iniparam.test(line) && line.match(iniparam)[1] === param) {
            value = line.match(iniparam)[2];
        }
    });

    return value;
}

class iniparser {

    constructor (path) {
        this.path = path;
    }
    
}
  • Related