How do you make a Javascript private method that is not redefined each time you call the constructor ?
As far as I know, in OOP-JS, private methods are methods defined in the “constructor method” of one’s “class”, called each time one instantiates a new “object”. I was thinking maybe a function declaration (i.e. function name(), as opposed to function expression var name = function()) would do the trick, but how can I be sure that the following code only declares my function once ?
function Tester() {
function test () {
console.log("executed");
}
}
var t1 = new Tester();
var t2 = new Tester();
You can’t (well, see below for a bit of wiggle room). But unless you’re going to have thousands of instances of
Tester, don’t worry about it too much; most engines probably reuse the underlying code across the multiple function objects that get created. (The code, mind; not the function object or the context it closes over, which must be unique and allocated each time. But they need not be large. Of course, quite a function functions are fairly small as well…)You can be sure that it doesn’t; it declares the function each time
Testeris called. Witness:Note that you can have functions that are private to the implementation, but not assigned to any instance of the object. The module pattern is handy for doing that:
There,
privateFunctionis completely private, accessible only to the code within the anonymous function. And there’s only one copy of it, but you can call it as though you were calling a method of aTesterinstance usingprivateFunction.call(this).Alternately, of course, since using
callis slightly slower than doing a normal call, you could just pass the instance as an argument:Of course, the extra cost of
callis only a problem if and when it’s a problem; unless you’re calling something hundreds of thousands of times in a tight loop, it’s unlikely to matter. So whether to usecallandthisor pass an argument would be primarily a style choice.