Is this the correct way to do private functions in Javascript / node.js?
Solution 1
Simplest way to have a private function is to just declare a function outside of the class. The prototype functions can still reference it perfectly fine, and pass their this
scope with .call()
function privateFunction() {
console.log(this.variable)
}
var MyClass = function () {
this.variable = 1;
}
MyClass.prototype.publicMethod = function() {
privateFunction.call(this);
}
var x = new MyClass();
x.publicMethod()
Solution 2
Update from May 17, 2019
In ESNext, which is the next specification of javascript, the support of private methods and attributes has been added.
To make an attribute or a function private, use the character #
.
class Foo {
#privateAttribute = 'hey';
showPrivate() {
console.log(this.#privateAttribute);
}
}
It has been implemented in node.js v12+.
You can configure babel to transpile it using the following plugin.
It requires babel v7.0.0+.
Solution 3
Whatever you will not add to module.exports
will be private for that module
and can not be accessed from outside of the module.
Also inside the controller store the reference of this
into a local variable
var self = this;
You can use revealing module pattern.
var myNameSpace = function() {
var current = null;
function init() {
…
}
function change() {
…
}
function verify() {
…
}
return{
init:init,
change:change
}
}();
module.exports = exports = myNameSpace;
This way init
and change
will be public
and verify will be private.
You can see Douglas Crockford style of making private members. http://javascript.crockford.com/private.html
Edit
Douglas Crockford's link has been changed now.
new link http://crockford.com/javascript/private.html
Solution 4
Yes you can make a private method, but it can't be part of the prototype
function ClassA()
{
var myvariable;
var private = function() // This is private method
{
myvariable = 0;
}
this.public = function() // This is public method
{
private();
}
}
Solution 5
Javascript Public Functions
ClassA.prototype.myFunction = function (string) {
//your logic
}
another type of public function
function ClassA(){
this.myvariable = 0;
var MyFunction3 = function () {
//your logic
};
this.MyFunction2 = function () {
//your logic
};
}
javascript Private Function
function ClassA() {
function MyFunction() {
//your logic
}
}
I would like to prefer to visit this link that will describe you every well.
aussieshibe
Updated on April 13, 2020Comments
-
aussieshibe about 4 years
A class I'm writing in node.js is as below:
module.exports = exports = function(){ return new ClassA() }; function ClassA(){ this.myvariable = 0; }
I have a function that I want to be private. To my understanding if the function is declared outside of the constructor, it will essentially be a static function which wouldn't be able to reference this.myvariable.
Is the correct way of dealing with this to declare the function within the constructor like this:
//within constructor this.myFunction = function myFunction(){ console.log(this.myvariable) }
Or is there a better way of doing it that doesn't leave me with a potentially huge constructor?
EDIT: It looks like I've misunderstood something here because the above code doesn't even work...
-
Kamil Kiełczewski over 3 yearsUsing arrow function you can also define private methods like properties
#privateMethod = () => { ... code ... }
-
Kerwin Sneijders about 3 yearsPrivate instance variables can be declared like:
this.#privateVar = 'asdf';
. You can also define private functions by just naming them with a#
at the start.#privateMethod() {...}
. Doesn't have to be using#a = () => {...}